我想說明一個警告對話框非活性類主要活動類getversion方法的方法,但每當我通過這個方法調用AlertDialog
它與下面的文本如何在另一個活動中顯示警報對話框?
public void AlertUpgrade(Activity activity) {
Log.e("AlertUpgrade", "Communicator.");
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(" Click OK to Upgrade Now ? ")
.setPositiveButton("OK" +
"", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.e("onClick", "AlertUpgrade");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=gsip.webgalaxy"));
marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
app_context.startActivity(marketIntent);
}
}).setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
return false;
}
return true;
}
});
builder.create().show();
}
public void AlertUpdate(Activity activity) {
Log.e("AlertUpdate", "Communicator.....");
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage("There is newer version of this application available, click OK to update now?").setPositiveButton("OK" +
"", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.e("onClick", "AlertUpdate");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("url"));
marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
app_context. startActivity(marketIntent);
}
}).setNegativeButton("Remind Later", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
}).setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
return false;
}
return true;
}
});
builder.create().show();
}
錯誤日誌
04-03 09:04:44.007: E/AndroidRuntime(2249): java.lang.RuntimeException: Unable to start activity ComponentInfo{gsip.webgalaxy/gsip.webgalaxy.ui.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.os.Handler.dispatchMessage(Handler.java:102)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.os.Looper.loop(Looper.java:136)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 09:04:44.007: E/AndroidRuntime(2249): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 09:04:44.007: E/AndroidRuntime(2249): at java.lang.reflect.Method.invoke(Method.java:515)
04-03 09:04:44.007: E/AndroidRuntime(2249): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 09:04:44.007: E/AndroidRuntime(2249): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 09:04:44.007: E/AndroidRuntime(2249): at dalvik.system.NativeStart.main(Native Method)
04-03 09:04:44.007: E/AndroidRuntime(2249): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.app.Dialog.show(Dialog.java:286)
04-03 09:04:44.007: E/AndroidRuntime(2249): at gsip.webgalaxy.ui.Communicator.getversion(Communicator.java:226)
04-03 09:04:44.007: E/AndroidRuntime(2249): at gsip.webgalaxy.ui.MainActivity.onCreate(MainActivity.java:460)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.app.Activity.performCreate(Activity.java:5231)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 09:04:44.007: E/AndroidRuntime(2249): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-03 09:04:44.007: E/AndroidRuntime(2249): ... 11 more
04-03 09:09:44.177: I/Process(2249): Sending signal. PID: 2249 SIG: 9
生成錯誤
剛剛發佈我們的代碼 –
我記得,有一個活動類型這是爲了給你一個看起來像啤酒的新活動RT。您可以使用此功能併發起打開您的特殊用途活動的意圖。自從我編寫Android代碼來記住具體細節時間太長,但我知道我已經完成了這個過程,而且非常簡單。 – mah