我正在尝试创建一个Aler Dialog但是当我点击按钮时我的应用程序崩溃了。不知道发生了什么,我该如何解决?
以下是代码:
Button deactivate = (Button) findViewById(R.id.deactivate);
deactivate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder( MyCollectionId.this )
.setTitle( "Invalid selection" )
.setMessage( "Drbr" )
.setPositiveButton( "Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog", "Positive");
}
})
.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog","Negative");
}
})
.show();
}
});
LogCat:
08-08 11:51:39.794: ERROR/AndroidRuntime(551): Uncaught handler: thread main exiting due to uncaught exception
08-08 11:51:39.824: ERROR/AndroidRuntime(551): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44c1d9f0 is not valid; is your activity running?
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewRoot.setView(ViewRoot.java:468)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.app.Dialog.show(Dialog.java:239)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.stampii.stampii.mystampii.MyCollectionId$3.onClick(MyCollectionId.java:67)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.View.performClick(View.java:2364)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.View.onTouchEvent(View.java:4179)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.widget.TextView.onTouchEvent(TextView.java:6541)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.View.dispatchTouchEvent(View.java:3709)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:123)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:521)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-08 11:51:39.824: ERROR/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
提前致谢!
答案 0 :(得分:1)
这一行:
new AlertDialog.Builder( MyCollectionId.this )
正在设置对话框的上下文以附加到活动。
您需要使用
YourActivity.this
new AlertDialog.Builder( YourActivity.this )
或将上下文设置为变量并使用:
Context mContext;
onCreate(){
mContext = this;
}
yourOnClick{
new AlertDialog.Builder( mContext )
}
如果这不是您的问题,那么您需要更多关于问题的“上下文”,您在哪里调用对话框中的Activity是什么活动状态。
答案 1 :(得分:1)
试试这个。
AlertDialog.Builder builder1=new AlertDialog.Builder(MyAlertdialogActivity.this);
builder1.setMessage("Please Press Ok");
builder1.setNeutralButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Ok is pressed", Toast.LENGTH_LONG).show();
}
});
builder1.show();
如果你发现它有用,请检查我的简陋博客。我已经在那里详细讨论过AlertDialog: http://androiddesk.wordpress.com/2012/01/25/dialog/