java - Mainactivity.java 中的错误

标签 java android eclipse error-handling

我有以下代码,但找不到消除这些错误的方法:

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)

这适用于第 17、18、19、20、21、22、23、24、25 行,其中包含:

findViewById(R.id.imageButton9).setOnClickListener(this);

在第 31 行(创建新类的行),我得到:

The nested type MainActivity cannot hide an enclosing type

这是我正在使用的代码:

package com.example.rome;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.imageButton1).setOnClickListener(this);
    findViewById(R.id.imageButton2).setOnClickListener(this);
    findViewById(R.id.imageButton3).setOnClickListener(this);
    findViewById(R.id.imageButton4).setOnClickListener(this);
    findViewById(R.id.imageButton5).setOnClickListener(this);
    findViewById(R.id.imageButton6).setOnClickListener(this);
    findViewById(R.id.imageButton7).setOnClickListener(this);
    findViewById(R.id.imageButton8).setOnClickListener(this);
    findViewById(R.id.imageButton9).setOnClickListener(this);



}

class MainActivity extends Activity implements View.OnClickListener {

    @Override
    public void onClick(View v){
      switch(v.getId()){
        case R.id.R.id.imagebutton1:
          startActivity(new Intent(telefoonnummers.class));
          break;
        case R.id.R.id.imagebutton2:
          startActivity(new Intent(telefoonnummers.class));
          break;
        //-- more cases --
        case R.id.R.id.imagebutton9:
              startActivity(new Intent(telefoonnummers.class));
              break;
      }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

最佳答案

删除错误的类定义:

class MainActivity extends Activity implements View.OnClickListener {

并将implements View.OnClickListener添加到真正的类定义中:

public class MainActivity extends Activity  implements View.OnClickListener {
//      Add this to the "real" MainActivity ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

花点时间确保您已正确闭合每个大括号 ({})。

关于java - Mainactivity.java 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15188196/

相关文章:

java - 如何在单台 linux 机器上创建 50000 个 tcp 连接?

java - File.exists()对于实际存在的文件(目录)返回false

android - 如何设置添加 child 时调用的onChildAdded

android - 如何在 Canvas 上保存图像和文本

java - Eclipse 的重构工具是否违反了 Java 语言规范?

c++ - 如何在 gtk 3.0 中构建示例项目

java - 安卓工作室 : Text cursor disappears/gone after open some other class or pasting text in different classes

java - 同步 textArea.clear() 后跟 textArea.setText() 不会清除文本

java - 如果我的 .txt 文件为空,如何修复扫描仪错误?

eclipse - 如何找出 Eclipse Xhtml Editor 挂起的原因