我正在尝试在我的程序中创建一个对话框。但是一个异常发生,活动已经完成,但我正在尝试显示一个带有已完成活动的上下文的对话框。但我在这里只使用一个活动。我我的活动里面有4个按钮点击。在其中一个按钮点击我给出警报框的代码。它显示我的应用程序崩溃
//这是代码
Allresidence.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( Allresidence.isChecked())
{
Log.e("allresidence","checked");
selectButton.setVisibility(View.VISIBLE);
Log.e("allresidence", "select button enabled");
//enable the button
selectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// the activity has finished but you are trying to display a dialog with a context of the finished activity
// if (Requestclass.!= null && !Requestclass.get().isFinishing()) {
Log.e(tag,"onclickworked");
AlertDialog alertDialog = new AlertDialog.Builder(
Requestclass.this).create();
Log.e(tag,"alertdialogobject created");
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
Log.e(tag, "set title for alertDialog");
// Setting Dialog Message
alertDialog.setMessage("listview");
Log.e(tag, "dialogmessage setted");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.gate_logo);
Log.e(tag, "icon setted");
alertDialog.show();
//
// }
});
在此按钮内单击我将提供创建对话框的代码。我对发生的事情感到困惑..... alertDialog.show()
方法不会在我的程序中执行..
下面是我的stacktrace
03-24 11:04:51.588: V/InputMethodManager(1242): START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{416e6320 VFE..... RF....ID 0,0-320,391} ic=null tba=android.view.inputmethod.EditorInfo@41a6eb08 controlFlags=#100
03-24 11:04:51.591: V/InputMethodManager(1242): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@41a16578 com.android.inputmethod.latin/.LatinIME #86}
03-24 11:04:51.667: I/SurfaceTextureClient(1242): [STC::queueBuffer] (this:0x51634fa8) fps:0.88, dur:1138.07, max:1138.07, min:1138.07
03-24 11:04:52.691: I/SurfaceTextureClient(1242): [STC::queueBuffer] (this:0x51634fa8) fps:0.98, dur:1024.43, max:1024.43, min:1024.43
03-24 11:04:52.715: V/Provider/Settings(1242): from settings cache , name = sound_effects_enabled , value = 0
03-24 11:04:52.716: E/allresidence(1242): checked
03-24 11:04:52.718: E/allresidence(1242): select button enabled
03-24 11:04:52.881: E/aaaa(1242): nokeyboard
03-24 11:04:55.376: V/Provider/Settings(1242): from settings cache , name = sound_effects_enabled , value = 0
03-24 11:04:55.377: E/Request class(1242): onclickworked
03-24 11:04:55.386: E/Request class(1242): alertdialogobject created
03-24 11:04:55.386: E/Request class(1242): set title for alertDialog
03-24 11:04:55.387: E/Request class(1242): dialogmessage setted
03-24 11:04:55.387: E/Request class(1242): icon setted
03-24 11:04:55.402: D/chenwei(1242): isHaveExternalSDCard=
03-24 11:04:55.404: D/chenwei(1242): isHaveExternalSDCard=
03-24 11:04:55.414: D/chenwei(1242): isHaveExternalSDCard=
03-24 11:04:55.436: D/chenwei(1242): isHaveExternalSDCard=
03-24 11:04:55.441: D/chenwei(1242): isHaveExternalSDCard=
03-24 11:04:55.444: D/chenwei(1242): isHaveExternalSDCard=
03-24 11:04:55.448: D/chenwei(1242): isHaveExternalSDCard=
03-24 11:04:55.451: D/chenwei(1242): isHaveExternalSDCard=
03-24 11:04:55.468: D/AndroidRuntime(1242): Shutting down VM
03-24 11:04:55.468: W/dalvikvm(1242): threadid=1: thread exiting with uncaught exception (group=0x413869a8)
03-24 11:04:55.475: E/AndroidRuntime(1242): FATAL EXCEPTION: main
03-24 11:04:55.475: E/AndroidRuntime(1242): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@41a14ce8 is not valid; is your activity running?
03-24 11:04:55.475: E/AndroidRuntime(1242): at android.view.ViewRootImpl.setView(ViewRootImpl.java:646)
03-24 11:04:55.475: E/AndroidRuntime(1242): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
03-24 11:04:55.475: E/AndroidRuntime(1242): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
03-24 11:04:55.475: E/AndroidRuntime(1242): at android.app.Dialog.show(Dialog.java:281)
03-24 11:04:55.475: E/AndroidRuntime(1242): at com.example.gate.Requestclass$4$1.onClick(Requestclass.java:296)
03-24 11:04:55.475: E/AndroidRuntime(1242): at android.view.View.performClick(View.java:4217)
03-24 11:04:55.475: E/AndroidRuntime(1242): at android.view.View$PerformClick.run(View.java:17502)
03-24 11:04:55.475: E/AndroidRuntime(1242): at android.os.Handler.handleCallback(Handler.java:800)
03-24 11:04:55.475: E/AndroidRuntime(1242): at android.os.Handler.dispatchMessage(Handler.java:100)
03-24 11:04:55.475: E/AndroidRuntime(1242): at android.os.Looper.loop(Looper.java:194)
03-24 11:04:55.475: E/AndroidRuntime(1242): at android.app.ActivityThread.main(ActivityThread.java:5371)
03-24 11:04:55.475: E/AndroidRuntime(1242): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 11:04:55.475: E/AndroidRuntime(1242): at java.lang.reflect.Method.invoke(Method.java:525)
03-24 11:04:55.475: E/AndroidRuntime(1242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-24 11:04:55.475: E/AndroidRuntime(1242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-24 11:04:55.475: E/AndroidRuntime(1242): at dalvik.system.NativeStart.main(Native Method)
预先感谢......任何帮助都会非常值得赞赏....
答案 0 :(得分:1)
这里的问题就像你没有在警告对话框中传递正确的上下文。 因此,要获得正确的上下文,请在单击侦听器中添加下面提到的代码。
Context context = getParent();
this.mDialog = new AlertDialog.Builder(context);