gpt4 book ai didi

android - 在 Android AlertDialog-Click 之后显示自定义对话框

转载 作者:行者123 更新时间:2023-11-30 04:52:10 24 4
gpt4 key购买 nike

我构建了一个显示三个项目的 AlertDialog:

AlertDialog.Builder builder = new AlertDialog.Builder(myActivity);
...
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(item==0){ //do stuff}
else if(item==1){//do other stuff}
else if(item==2){//show other dialog}
...

现在我想在用户选择第三项(item==2)时显示另一个对话框。我的方法如下:

Dialog otherDialog = new Dialog(myActivity.savedApplicationContext);
otherDialog.setContentView(R.layout.other_layout);
otherDialog.setTitle(getString(R.string.update_dialog));
dialog.dismiss(); //dismiss old dialog
otherDialog.show();

这是行不通的 :( 抛出异常:

06-03 12:21:32.684: ERROR/AndroidRuntime(507): Uncaught handler: thread main exiting due to uncaught exception
06-03 12:21:32.834: ERROR/AndroidRuntime(507): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at android.view.ViewRoot.setView(ViewRoot.java:472)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at android.app.Dialog.show(Dialog.java:239)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at com.dapr.altfuelloc.activity.DetailsActivity$1$1.onClick(DetailsActivity.java:96)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at android.widget.ListView.performItemClick(ListView.java:3285)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at android.os.Handler.handleCallback(Handler.java:587)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at android.os.Looper.loop(Looper.java:123)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at java.lang.reflect.Method.invoke(Method.java:521)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-03 12:21:32.834: ERROR/AndroidRuntime(507): at dalvik.system.NativeStart.main(Native Method)

我在 stackoverflow/inet 上搜索过,但解决方案是将 Activity 的 applicationContext 传递给 Dialog(我在这种情况下这样做,我将 Activity applicationContext 的引用保存在私有(private)变量中: myActivity.savedApplicationContext

最佳答案

解决了问题。

代替savedApplicationContext = getApplicationContext();

我用过savedApplicationContext = this;

现在可以了:-)

关于android - 在 Android AlertDialog-Click 之后显示自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2965856/

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