错误消息告诉我没有onButton1Click方法,但是在使用onButton1Click时我在之前的活动中从未得到过这个,所以idk是什么让这个场景有所不同。
java.lang.IllegalStateException: Could not find a method onButton1Click(View) in the activity class com.example.chiozokamalu.brainnoodles.Results1 for onClick handler on view class android.widget.Button with id 'homePageButton'
at android.view.View$1.onClick(View.java:2131)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: onButton1Click
at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
at java.lang.Class.getMethod(Class.java:962)
at android.view.View$1.onClick(View.java:2124)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
这是带有相应错误的类文件
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class Results1 extends MainActivity implements View.OnClickListener {
TextView resultView1, resultView2, resultView3, resultView4, resultView5,
resultView6, resultView7, resultView8, resultView9, resultView10;
Button homePageButton;
String[] correctA1 = {
"2", //Array 0
"1.25", //Array1
"-1 degrees Fahrenheit", //Array 2
"1 hour", //Array 3
"12 months", //Array 4
"100", //Array 5
"They weigh the same", //Array 6
"Their",//Array 7
"More than 20", //Array 8
"The denser the cloud, the darker the atmosphere"// Array 9
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.results1);
resultView1 = ((TextView) findViewById(R.id.resultView1));
resultView2 = ((TextView) findViewById(R.id.resultView2));
resultView3 = ((TextView) findViewById(R.id.resultView3));
resultView4 = ((TextView) findViewById(R.id.resultView4));
resultView5 = ((TextView) findViewById(R.id.resultView5));
resultView6 = ((TextView) findViewById(R.id.resultView6));
resultView7 = ((TextView) findViewById(R.id.resultView7));
resultView8 = ((TextView) findViewById(R.id.resultView8));
resultView9 = ((TextView) findViewById(R.id.resultView9));
resultView10 = ((TextView) findViewById(R.id.resultView10));
resultView1.setText("1. " +correctA1[0]);
resultView2.setText("2. " +correctA1[1]);
resultView3.setText("3. " +correctA1[2]);
resultView4.setText("4. " +correctA1[3]);
resultView5.setText("5. " +correctA1[4]);
resultView6.setText("6. " +correctA1[5]);
resultView7.setText("7. " +correctA1[6]);
resultView8.setText("8. " +correctA1[7]);
resultView9.setText("9. " +correctA1[8]);
resultView10.setText("10. " +correctA1[9]);
}
@Override public void onClick(View v) {
homePageButton = ((Button) findViewById(R.id.homePageButton));
homePageButton.setOnClickListener(this);
boolean yes1 = true;
boolean yes2 = true;
startActivity(new Intent("android.intent.action.MAIN"));
}
}
答案 0 :(得分:0)
检查您的" homePageButton"有一个在XML文件中定义的处理程序,即" android:onClick"
答案 1 :(得分:0)
检查此活动的xml布局。您可能已将onButton1Click
指定为视图的onClick处理程序,但未在您的活动中实现该方法。
答案 2 :(得分:0)
你的布局文件(results1.xml)中有一个android:onClick =“onButton1Click”,但你的java代码中没有相应的方法来处理onclick。
修改Results1类代码并添加如下所示的方法:
public void onButton1Click(View v){
//do something here
}
这可以解决您的问题。