尝试创建自定义对话框。我创建了一个新的xml布局文件名dialog_result: 然后在我的GuessActivity上,我有这个代码:
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();
}
}
对于我的生活,我不知道应用程序崩溃的原因。它一直工作到log.d(“右”,“右”)和/或log.d(“错误”,“错误”),然后它在调用resultDialogBox时崩溃。
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
答案 0 :(得分:0)
错误日志表明你在第54行有NullPointerException
。你有检查吗?
我认为有问题的代码是
result.setText("Correct");
它为null,因为您通过
实例化result
TextView result = (TextView) findViewById(R.id.resulttext);
findViewById
是在活动上调用的方法,而不是在Dialog
上。如果您的活动没有Null
,则返回TextView
。
根据documentation of Dialogs,您不应直接使用Dialog
。请改用AlertDialog.Builder
:
// 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();
答案 1 :(得分:0)
您没有在对话框中设置内容视图。试试这个。
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();
}