在Mainactivity.java错误错误、Mainactivity、java

2023-09-07 04:08:33 作者:放肆的青春゛つ

我有以下的code,我无法找到一种方法来摆脱这些错误的:

  

在类型视图的方法setOnClickListener(View.OnClickListener)不适用于参数(MainActivity)

这适用于线17,18,19,20,21,22,23,24,含25:

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

在第31行(在其中创建新类线),我得到:

  

嵌套类型MainActivity无法隐藏封闭类型

50 个 Java 开发常见错误及规避技巧 Part 1

这是code我的工作:

 包com.example.rome;进口android.os.Bundle;进口android.app.Activity;进口android.view.Menu;进口android.widget.EditText;进口android.widget.Button;进口android.view.View;进口android.widget.Toast;公共类MainActivity延伸活动{@覆盖保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);    findViewById(R.id.imageButton1).setOnClickListener(本);    findViewById(R.id.imageButton2).setOnClickListener(本);    findViewById(R.id.imageButton3).setOnClickListener(本);    findViewById(R.id.imageButton4).setOnClickListener(本);    findViewById(R.id.imageButton5).setOnClickListener(本);    findViewById(R.id.imageButton6).setOnClickListener(本);    findViewById(R.id.imageButton7).setOnClickListener(本);    findViewById(R.id.imageButton8).setOnClickListener(本);    findViewById(R.id.imageButton9).setOnClickListener(本);}类MainActivity扩展活动实现View.OnClickListener {    @覆盖    公共无效的onClick(视图v){      开关(v.getId()){        案例R.id.R.id.imagebutton1:          startActivity(新意图(telefoonnummers.class));          打破;        案例R.id.R.id.imagebutton2:          startActivity(新意图(telefoonnummers.class));          打破;        //  - 更多的情况下 -         案例R.id.R.id.imagebutton9:              startActivity(新意图(telefoonnummers.class));              打破;      }    }}@覆盖公共布尔onCreateOptionsMenu(菜单菜单){    //充气菜单;如果是present这增加了项目操作栏。    。getMenuInflater()膨胀(R.menu.activity_main,菜单);    返回true;}} 

解决方案

删除错误的类定义:

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