gpt4 book ai didi

java - AsyncTask 使应用程序崩溃

转载 作者:搜寻专家 更新时间:2023-10-30 20:40:48 25 4
gpt4 key购买 nike

我正在尝试使用 .php 联系我的数据库ASYNC 第一次运行时它工作正常,但我第二次运行它(没有退出应用程序)应用程序崩溃(无法从 LogCat 捕获错误消息)

而且出于某种原因,它根本不会进入 while 循环。我以前写过很多次这样的代码,结果都很好,但这次不行。

代码:

class LOAD_USERS extends AsyncTask<String, Void, Void>
{
//Internet Input
URL url;
InputStream iS;
InputStreamReader iSR;
BufferedReader r;

//Variables
List<String> Users = new ArrayList<String>();
public String s = "";
public String DOWNLOAD_SUCCESS = "fail";
String charset = "iso-8859-1";

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MenuActivity.this);
pDialog.setMessage("Letar efter spelare");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

@Override
protected Void doInBackground(String... sUrl) {
try{
url = new URL(sUrl[0]);
iS = url.openStream();
iSR = new InputStreamReader(iS, charset);
r = new BufferedReader(iSR);

Users.clear();

while((s = r.readLine()) != null)
{
Users.add(s);
DOWNLOAD_SUCCESS = "success";
}
}catch(Exception e)
{
Log.e(e.toString(),e.toString());
}

return null;
}

@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
pDialog.dismiss();
if(DOWNLOAD_SUCCESS.equalsIgnoreCase("success"))
{
MenuActivity.this.CheckValidation();
}
else if(DOWNLOAD_SUCCESS.equalsIgnoreCase("fail"))
{
Toast.makeText(getBaseContext(), "Check Your Internet Connection", Toast.LENGTH_SHORT).show();
}

DownloadComplete = true;
}
}

如果你能发现我的错误,那就太好了。谢谢!

最佳答案

来自AsyncTask documentation (线程规则部分):

The task can be executed only once (an exception will be thrown if a second execution is attempted.)

关于java - AsyncTask 使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21101352/

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