我有以下代码,但找不到消除这些错误的方法:
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/