gpt4 book ai didi

android - 执行 AlertDialog.Builder create() 方法时应用程序崩溃 - Android

转载 作者:太空狗 更新时间:2023-10-29 16:25:14 34 4
gpt4 key购买 nike

我正在 LG Eve 手机上测试我的应用程序。我有一个应用程序试图从网上下载一些东西,当它抛出异常时,它应该启动一个警告对话框,说有一个错误。当手机没有 wifi 信号时,程序会在 builder.create() 处崩溃(见下面的代码)。然而,当有 wifi 信号时,异常是由其他东西引发的(例如,url 中的拼写错误),对话框将按预期方式启动。关于为什么会这样的任何线索?

onCreateDialog 代码:

@Override
protected Dialog onCreateDialog(int id){

Dialog d = null;
switch (id){

case DIALOG_DATA_ERROR_ID:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getResources().getString(R.string.error_data));
builder.setCancelable(false);
builder.setNeutralButton("OK", new DialogInterface.OnClickListener(){

public void onClick(DialogInterface d, int id){

d.cancel();
}
});
d = builder.create();
break;
}
return d;
}

调用 showDialog 的 AsyncTask 代码:

private static class DownloadJSONTask extends AsyncTask<String, Void, String>{


private ProgressDialog dialog;
private Activity parent;
private JSONParserInterface jsonParser;

public DownloadJSONTask(Activity parent, JSONParserInterface jsonParser){
this.parent = parent;
this.jsonParser = jsonParser;
}

protected void onPreExecute(){

dialog = ProgressDialog.show(parent, "Loading", "Please Wait...", true);

}

protected String doInBackground (String... urls){

try {

return HttpHelper.getResponse(urls[0]);

}catch (Exception e){
dialog.cancel();
parent.showDialog(BoomSetListActivity.DIALOG_DATA_ERROR_ID);
}

return null;

}

protected void onPostExecute(String json){
dialog.cancel();
if (jsonParser != null) jsonParser.parse(json);
}


}

最佳答案

不在 doInBackground 显示对话框。该方法不在 UI 线程上运行。尝试在 onPostExecute 或 onProgressUpdate 显示错误对话框。

关于android - 执行 AlertDialog.Builder create() 方法时应用程序崩溃 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3517231/

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