gpt4 book ai didi

android - 从非 Activity 中完成 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:35:26 25 4
gpt4 key购买 nike

我正在开发一个应用程序。我想从第一个 Activity 中调用一个警报对话框。警报对话框是在另一个类中编写的,该类是非 Activity 类。应用程序的需要是调用 Activity 应该从编写在非 Activity 类中的警报结束。调用警报的代码如下。

从第一个 Activity 调用警报:

public class Activity1 extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
}// End of onCreate.

/** Back Button. */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
String message = "Do you Want to Exit.";
Utility.callAlert(message, getApplicationContext());
return true;
}
return super.onKeyDown(keyCode, event);
}
}

具有警告对话框的被调用类是:

public class Utility {
// callAlert method to display alert, when exception occur.
public static void callAlert(String message, final Context context){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("MyApplication.");
alertDialogBuilder.setMessage(message);
alertDialogBuilder.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
((Activity1)context.getApplicationContext()).finish();
} //end onClick.
}); // end alertDialog.setButton.
alertDialogBuilder.show();
}
}

但这给我的错误是:

02-25 17:00:45.961: D/AndroidRuntime(444): Shutting down VM
02-25 17:00:45.961: W/dalvikvm(444): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-25 17:00:45.971: E/AndroidRuntime(444): FATAL EXCEPTION: main
02-25 17:00:45.971: E/AndroidRuntime(444): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-25 17:00:45.971: E/AndroidRuntime(444): at android.view.ViewRoot.setView(ViewRoot.java:531)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.app.Dialog.show(Dialog.java:241)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
02-25 17:00:45.971: E/AndroidRuntime(444): at com.xxx.myapp.specialclasses.Utility.callAlert(Utility.java:25)
02-25 17:00:45.971: E/AndroidRuntime(444): at com.xxx.myapp.activities.Activity1.onKeyDown(Activity1.java:149)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.view.KeyEvent.dispatch(KeyEvent.java:1256)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
02-25 17:00:45.971: E/AndroidRuntime(444): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.os.Looper.loop(Looper.java:123)
02-25 17:00:45.971: E/AndroidRuntime(444): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-25 17:00:45.971: E/AndroidRuntime(444): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 17:00:45.971: E/AndroidRuntime(444): at java.lang.reflect.Method.invoke(Method.java:507)
02-25 17:00:45.971: E/AndroidRuntime(444): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-25 17:00:45.971: E/AndroidRuntime(444): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-25 17:00:45.971: E/AndroidRuntime(444): at dalvik.system.NativeStart.main(Native Method)
02-25 17:05:46.061: I/Process(444): Sending signal. PID: 444 SIG: 9

最佳答案

代替:

Utility.callAlert(message, getApplicationContext());

使用:

Utility.callAlert(message, Activity1.this);

public void onClick(DialogInterface dialog, int which) {
((Activity)context).finish();
}

关于android - 从非 Activity 中完成 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15066378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com