且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Mainactivity.java错误

更新时间:2023-11-16 17:28:28

删除错误的类定义:

 类MainActivity扩展活动实现View.OnClickListener {

和增加实现View.OnClickListener 来真正的类定义:

 公共类MainActivity扩展活动实现View.OnClickListener {
//这个添加到真正的MainActivity ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

花点时间,以确保您正确每逢括号( {} )。

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)

This applies to the lines 17, 18, 19, 20, 21, 22, 23, 24, 25 containing:

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

In line 31 (the line where the new class is created), I get:

The nested type MainActivity cannot hide an enclosing type

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:

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 ({}).