I have two buttons in my dialog box, which is coded in the xml file:

 <Button
    android:id="@+id/continuegame"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/resulttext"
    android:layout_marginRight="26dp"
    android:layout_marginTop="23dp"
    android:layout_toLeftOf="@+id/backmain"
    android:onClick="continueGame"
    android:text="@string/label_continue" />

<Button
    android:id="@+id/backmain"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/continuegame"
    android:layout_alignBottom="@+id/continuegame"
    android:layout_alignParentRight="true"
    android:layout_marginRight="52dp"
    android:onClick="backToMain"
    android:text="@string/label_mainmenu" />

I have successfully call the dialog box but the problem is when I click either the continue or back to main menu button, the app crashes.

this is my code in my GuessActivity.java:

//when Submit button is clicked this method is called
public void checkAnswer(View view){
    EditText userinput = (EditText) findViewById(R.id.inputLetter);
    String temp = userinput.getText().toString();
    char userletter = temp.charAt(0);

    Bundle fromplay = getIntent().getExtras();
    String temp2 = fromplay.getString("missing");
    char missingletter = temp2.charAt(0);

    Log.d ("temp2", temp2);

    if(userletter == missingletter){
        Log.d ("right", "right");
        correct = 1;
        //will call a dialog box
        resultDialogBox(correct);
    } else {
        Log.d ("wrong", "wrong");
        correct = 0;
        //will call a dialog box
        resultDialogBox(correct);
    }


}

//the dialog box
public void resultDialogBox(int correct){
    this.correct = correct;
    Dialog resultbox = new Dialog(GuessActivity.this);
    resultbox.setContentView(R.layout.dialog_result);
    resultbox.setCanceledOnTouchOutside(false);
    TextView result = (TextView) resultbox.findViewById(R.id.resulttext);


    if(correct == 1){
        result.setText("Correct!");
    } else {
        result.setText("Wrong!");
    }

    resultbox.show();

}

//method of the continue button
public void continueGame(View view){
    Intent con = new Intent(GuessActivity.this, PlayActivity.class);
    GuessActivity.this.startActivity(con);
}

//method of the back to main button
public void backToMain(View view){
    Intent backtomain = new Intent(GuessActivity.this, MainActivity.class);
    GuessActivity.this.startActivity(backtomain);
}

the logcat:

04-28 13:54:48.860: W/dalvikvm(24493): threadid=1: thread exiting with uncaught exception (group=0x4161c8b0)
04-28 13:54:48.870: E/AndroidRuntime(24493): FATAL EXCEPTION: main
04-28 13:54:48.870: E/AndroidRuntime(24493): java.lang.IllegalStateException: Could not find a method continueGame(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'continuegame'
04-28 13:54:48.870: E/AndroidRuntime(24493):    at android.view.View$1.onClick(View.java:3801)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at android.view.View.performClick(View.java:4421)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at android.view.View$PerformClick.run(View.java:17903)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at android.os.Handler.handleCallback(Handler.java:730)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at android.os.Looper.loop(Looper.java:213)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at android.app.ActivityThread.main(ActivityThread.java:5225)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at java.lang.reflect.Method.invokeNative(Native Method)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at java.lang.reflect.Method.invoke(Method.java:525)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at dalvik.system.NativeStart.main(Native Method)
04-28 13:54:48.870: E/AndroidRuntime(24493): Caused by: java.lang.NoSuchMethodException: continueGame [class android.view.View]
04-28 13:54:48.870: E/AndroidRuntime(24493):    at java.lang.Class.getConstructorOrMethod(Class.java:423)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at java.lang.Class.getMethod(Class.java:787)
04-28 13:54:48.870: E/AndroidRuntime(24493):    at android.view.View$1.onClick(View.java:3794)
04-28 13:54:48.870: E/AndroidRuntime(24493):    ... 11 more
04-28 13:54:50.441: I/Process(24493): Sending signal. PID: 24493 SIG: 9
04-28 13:54:50.692: D/libEGL(24827): loaded /vendor/lib/egl/libEGL_adreno.so
04-28 13:54:50.692: D/libEGL(24827): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
04-28 13:54:50.692: D/libEGL(24827): loaded /vendor/lib/egl/libGLESv2_adreno.so
04-28 13:54:50.692: I/Adreno-EGL(24827): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build:  (CL4169980)
04-28 13:54:50.692: I/Adreno-EGL(24827): OpenGL ES Shader Compiler Version: 17.01.10.SPL
04-28 13:54:50.692: I/Adreno-EGL(24827): Build Date: 12/01/13 Sun
04-28 13:54:50.692: I/Adreno-EGL(24827): Local Branch: 
04-28 13:54:50.692: I/Adreno-EGL(24827): Remote Branch: 
04-28 13:54:50.692: I/Adreno-EGL(24827): Local Patches: 
04-28 13:54:50.692: I/Adreno-EGL(24827): Reconstruct Branch: 
04-28 13:54:50.732: D/OpenGLRenderer(24827): Enabling debug mode 0
有帮助吗?

解决方案

Try this..

public void resultDialogBox(int correct){
    this.correct = correct;
    Dialog resultbox = new Dialog(GuessActivity.this);
    resultbox.setContentView(R.layout.dialog_result);
    resultbox.setCanceledOnTouchOutside(false);
    TextView result = (TextView) resultbox.findViewById(R.id.resulttext);
    Button continuegame = (Button) resultbox.findViewById(R.id.continuegame);
    Button backmain = (Button) resultbox.findViewById(R.id.backmain);

    if(correct == 1){
        result.setText("Correct!");
    } else {
        result.setText("Wrong!");
    }

    continuegame.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
                  Intent con = new Intent(GuessActivity.this, PlayActivity.class);
                  GuessActivity.this.startActivity(con);
           }
    });

    backmain.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
                  Intent backtomain = new Intent(GuessActivity.this, MainActivity.class);
                  GuessActivity.this.startActivity(backtomain);
           }
    });

    resultbox.show();

}

and remove both android:onClick="backToMain" and android:onClick="continueGame" from XML

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top