Trying to create a custom dialog box. I created a new xml layout file name dialog_result: Then on my GuessActivity, I have this code:

public class GuessActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_guess);

}

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");
        resultDialogBox();
    } else {
        Log.d ("wrong", "wrong");
        resultDialogBox();
    }


}

public void resultDialogBox(){
    Dialog resultbox = new Dialog(GuessActivity.this);
    TextView result = (TextView) findViewById(R.id.resulttext);
    resultbox.setContentView(R.layout.dialog_result);

    result.setText("Correct");

    resultbox.show();
}

}

For the life of me, I don't know why the app is crashing. It's working until the log.d ("right", "right") and or log.d("wrong", "wrong"),; then it crashes when it calls the resultDialogBox.

the logcat:

04-28 10:43:33.705: D/temp2(7079): n
04-28 10:43:33.705: D/right(7079): right
04-28 10:43:33.715: D/AndroidRuntime(7079): Shutting down VM
04-28 10:43:33.715: W/dalvikvm(7079): threadid=1: thread exiting with uncaught exception (group=0x4161c8b0)
04-28 10:43:33.725: E/AndroidRuntime(7079): FATAL EXCEPTION: main
04-28 10:43:33.725: E/AndroidRuntime(7079): java.lang.IllegalStateException: Could not execute method of the activity
04-28 10:43:33.725: E/AndroidRuntime(7079):     at android.view.View$1.onClick(View.java:3814)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at android.view.View.performClick(View.java:4421)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at android.view.View$PerformClick.run(View.java:17903)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at android.os.Handler.handleCallback(Handler.java:730)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at android.os.Looper.loop(Looper.java:213)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at android.app.ActivityThread.main(ActivityThread.java:5225)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at java.lang.reflect.Method.invoke(Method.java:525)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at dalvik.system.NativeStart.main(Native Method)
04-28 10:43:33.725: E/AndroidRuntime(7079): Caused by: java.lang.reflect.InvocationTargetException
04-28 10:43:33.725: E/AndroidRuntime(7079):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at java.lang.reflect.Method.invoke(Method.java:525)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at android.view.View$1.onClick(View.java:3809)
04-28 10:43:33.725: E/AndroidRuntime(7079):     ... 11 more
04-28 10:43:33.725: E/AndroidRuntime(7079): Caused by: java.lang.NullPointerException
04-28 10:43:33.725: E/AndroidRuntime(7079):     at com.johnyeung.letterhunter.GuessActivity.resultDialogBox(GuessActivity.java:54)
04-28 10:43:33.725: E/AndroidRuntime(7079):     at com.johnyeung.letterhunter.GuessActivity.checkAnswer(GuessActivity.java:40)
04-28 10:43:33.725: E/AndroidRuntime(7079):     ... 14 more
04-28 10:43:37.899: D/libEGL(7200): loaded /vendor/lib/egl/libEGL_adreno.so
04-28 10:43:37.899: D/libEGL(7200): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
04-28 10:43:37.899: D/libEGL(7200): loaded /vendor/lib/egl/libGLESv2_adreno.so
04-28 10:43:37.899: I/Adreno-EGL(7200): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build:  (CL4169980)
04-28 10:43:37.899: I/Adreno-EGL(7200): OpenGL ES Shader Compiler Version: 17.01.10.SPL
04-28 10:43:37.899: I/Adreno-EGL(7200): Build Date: 12/01/13 Sun
04-28 10:43:37.899: I/Adreno-EGL(7200): Local Branch: 
04-28 10:43:37.899: I/Adreno-EGL(7200): Remote Branch: 
04-28 10:43:37.899: I/Adreno-EGL(7200): Local Patches: 
04-28 10:43:37.899: I/Adreno-EGL(7200): Reconstruct Branch: 
04-28 10:43:37.940: D/OpenGLRenderer(7200): Enabling debug mode 0
有帮助吗?

解决方案

You are not setting the content view on dialog. Try this.

public void resultDialogBox(){

Dialog resultbox = new Dialog(GuessActivity.this);
resultbox.setContentView(R.layout.dialog_result);
TextView result = (TextView)resultbox.findViewById(R.id.resulttext);
result.setText("Correct");

resultbox.show();

}

其他提示

The error log states that your have NullPointerException on line 54. Did you check it?

I think the code having problem is

result.setText("Correct");

It is null because you instantiate the result by

TextView result = (TextView) findViewById(R.id.resulttext);

findViewById is a method called on the activity, not on the Dialog. So it return Null if your activity does not have such TextView.

And you should not use the Dialog directly, according to the documentation of Dialogs. Use AlertDialog.Builder instead:

// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
       .setTitle(R.string.dialog_title);

// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top