本文介绍了在Mainactivity.java错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的code,我无法找到一种方法来摆脱这些错误的:
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:
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 ({}
).
这篇关于在Mainactivity.java错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!