我的应用程序在尝试加载新的活动页面时崩溃

问题描述:

错误消息告诉我没有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

将'onClick'方法重命名为'onButton1Click',并移除'@ Override'注解。你不需要实现'View.OnClickListener' – SorryForMyEnglish 2015-02-24 19:36:49

+0

@SorryForMyEnglish确定这个问题解决了它,只要我知道为什么我的其他活动不会给出任何错误 – 2015-02-24 20:03:26

+0

我认为由于其他活动,你可以找到这个代码'按钮。 setOnClickListener(this)' – SorryForMyEnglish 2015-02-24 20:45:37

检查类文件的“homePageButton”有XML文件,即在规定的处理程序“安卓的onClick”

+0

它确实........ – 2015-02-24 19:49:50

+0

因此,要么定义一个相应的处理程序或删除onClick – 2015-02-24 20:11:47

检查你的XML此活动的布局。您可能已指定onButton1Click作为视图的onClick处理程序,但未在您的活动中实施该方法。

+0

我不知道如何做到这一点,而不会搞乱。我有另一个活动,我复制了相同的按钮点击格式。我从来没有在Class文件中编写onButton1Click,但一切正常。 – 2015-02-24 19:56:26

+0

查看布局XML文件(R.layout.results1)。寻找一个'android:onClick'字段。尽管该方法不存在于您的活动中,您可能会尝试将onClick事件分配给onButton1Click()。 – dcarr622 2015-02-24 21:25:18

在布局文件(results1.xml)中有一个android:onClick =“onButton1Click”,但在java代码中没有相应的方法来处理onclick。

修改您的结果:1类代码并添加一个方法类似如下:

public void onButton1Click(View v){ 
    //do something here 
} 

这应该解决您的问题。