首页 > 解决方案 > 在 Android Studio 中运行我的代码时出现顶级声明错误

问题描述

我写了这段代码,运行程序时出现错误

期待顶级声明

在关键字extends和其他87个地方附近。它还将诸如 int、float 之类的关键字显示为无法识别。

完整代码:

package com.example.exno1

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView

class MainActivity extends AppCompatActivity{
    int ch=1
    float font=30
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        finalTextView t= (TextView) findViewById(R.id.textView)
        Button b1= (Button) findViewById(R.id.button1)
        b1.setOnClickListener(newView.OnClickListener {
            @Override
            Public void onClick(View v) {
                t.setTextSize(font)
                font = font + 5
                if(font == 50)
                    font = 30
            }
        })
        Button b2= (Button) findViewById(R.id.button2)
        b2.setOnClickListener(newView.OnClickListener {
            @Override
            public void onClick(View v) {
                switch(ch) {
                    case 1:
                    t.setTextColor(Color.RED)
                    break
                    case 2:
                    t.setTextColor(Color.GREEN)
                    break
                    case 3:
                    t.setTextColor(Color.BLUE)
                    break
                    case 4:
                    t.setTextColor(Color.CYAN)
                    break
                    case 5:
                    t.setTextColor(Color.YELLOW)
                    break
                    case 6:
                    t.setTextColor(Color.MAGENTA)
                    break
                }
                ch++
                if(ch == 7)
                    ch = 1
            }
        })
    }
}

标签: javaandroid

解决方案


它是Java。你忘了分号;


推荐阅读