简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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