mainactivity.java r,在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 ({}).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值