简体   繁体   English

Mainactivity.java中的错误

[英]Errors in Mainactivity.java

I have the following code and I can't find a way to get rid of these errors: 我有以下代码,但找不到消除这些错误的方法:

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity) 视图类型中的方法setOnClickListener(View.OnClickListener)不适用于参数(MainActivity)

This applies to the lines 17, 18, 19, 20, 21, 22, 23, 24, 25 containing: 这适用于第17、18、19、20、21、22、23、24、25行,其中包含:

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

In line 31 (the line where the new class is created), I get: 在第31行(创建新类的行)中,我得到:

The nested type MainActivity cannot hide an enclosing type 嵌套类型MainActivity无法隐藏封闭类型

This is the code I'm working with: 这是我正在使用的代码:

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;
}

}

Remove the errant class definition: 删除错误的类定义:

class MainActivity extends Activity implements View.OnClickListener {

And add implements View.OnClickListener to the real class definition: 并将implements View.OnClickListener添加到真实的类定义中:

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

Take a moment to make sure you have properly closed every brace ( {} ). 请花一点时间确保已正确合上每个大括号( {} )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM