gpt4 book ai didi

android - 警报对话框生成器停止响应

转载 作者:行者123 更新时间:2023-11-29 14:52:43 24 4
gpt4 key购买 nike

我在显示 AlertDialog 时遇到问题。每当我运行代码时,应用程序都会变得没有响应并强制关闭。附上我的代码:

public void displayList(View view){     new AsyncTask<String, Void, List<Address>>() {          protected List<Address> doInBackground(String... params) {               /*  some operations  */               return null;          }          protected void onPostExecute(List<Address> result) {               if(result.size() == 0){                    /*  do something  */               } else {                    /*  some code  */                    AlertDialog.Builder alert = new AlertDialog.Builder(this);                    alert.setMessage(sometext);                    alert.show();               }          }     }.execute(destination);}

这是我的 logCat

11-20 20:26:56.848: E/AndroidRuntime(29789): FATAL EXCEPTION: main11-20 20:26:56.848: E/AndroidRuntime(29789): java.lang.NullPointerException11-20 20:26:56.848: E/AndroidRuntime(29789):    at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)11-20 20:26:56.848: E/AndroidRuntime(29789):    at android.app.AlertDialog$Builder.(AlertDialog.java:359)11-20 20:26:56.848: E/AndroidRuntime(29789):    at com.ceria.tuntun.MainActivity$5.onPostExecute(MainActivity.java:201)11-20 20:26:56.848: E/AndroidRuntime(29789):    at com.ceria.tuntun.MainActivity$5.onPostExecute(MainActivity.java:1)11-20 20:26:56.848: E/AndroidRuntime(29789):    at android.os.AsyncTask.finish(AsyncTask.java:602)11-20 20:26:56.848: E/AndroidRuntime(29789):    at android.os.AsyncTask.access$600(AsyncTask.java:156)11-20 20:26:56.848: E/AndroidRuntime(29789):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)11-20 20:26:56.848: E/AndroidRuntime(29789):    at android.os.Handler.dispatchMessage(Handler.java:99)11-20 20:26:56.848: E/AndroidRuntime(29789):    at android.os.Looper.loop(Looper.java:137)11-20 20:26:56.848: E/AndroidRuntime(29789):    at android.app.ActivityThread.main(ActivityThread.java:4511)11-20 20:26:56.848: E/AndroidRuntime(29789):    at java.lang.reflect.Method.invokeNative(Native Method)11-20 20:26:56.848: E/AndroidRuntime(29789):    at java.lang.reflect.Method.invoke(Method.java:511)11-20 20:26:56.848: E/AndroidRuntime(29789):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)11-20 20:26:56.848: E/AndroidRuntime(29789):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)11-20 20:26:56.848: E/AndroidRuntime(29789):    at dalvik.system.NativeStart.main(Native Method)

我不知道这是怎么回事。我已经多次使用 AlertDialog 并且想知道为什么这个不能工作。

谢谢

最佳答案

AlertDialog.Builder alert = new AlertDialog.Builder(this);

因为“this”指的是异步任务的匿名内部类,它需要来自 Activity 的上下文

关于android - 警报对话框生成器停止响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13474199/

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