在AsyncTask Android中显示Toast时出错
在我的RSS阅读器应用程序中,我试图检查输入的RSS url是否有效。如果是这样,我将把它保存在SQLite数据库中。我的方法是使用AsyncTask:在AsyncTask Android中显示Toast时出错,android,android-asynctask,toast,Android,Android Asynctask,Toast,在我的RSS阅读器应用程序中,我试图检查输入的RSS url是否有效。如果是这样,我将把它保存在SQLite数据库中。我的方法是使用AsyncTask: class mAsyncTask extends AsyncTask<String, Integer, String> { private boolean succeed = false; public mAsyncTask() { } protected String doInBackg
class mAsyncTask extends AsyncTask<String, Integer, String>
{
private boolean succeed = false;
public mAsyncTask()
{
}
protected String doInBackground(String... urls)
{
try
{
URL sourceUrl = new URL(sourceUrlString);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(global.getInputStream(sourceUrl), "UTF_8");
// if it reach here, it means the XML is in a valid
// format
if (xpp.getEventType() == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("title"))
sourceTitle = xpp.nextText();
succeed = true;
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Input RSS feed is not in the valid format...", Toast.LENGTH_LONG).show();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), "Unknown error occurred..", Toast.LENGTH_LONG).show();
}
return "Complete doInBackground";
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress)
{
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
if (succeed == true)
{
db.addSource(new Sources("", sourceTitle, sourceUrlString));
//refresh
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
}
类mAsyncTask扩展异步任务
{
私有布尔成功=假;
公共mAsyncTask()
{
}
受保护的字符串doInBackground(字符串…URL)
{
尝试
{
URL sourceUrl=新URL(sourceUrlString);
XmlPullParserFactory工厂=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp=factory.newPullParser();
//我们将从输入流中获取XML
setInput(global.getInputStream(sourceUrl),“UTF_8”);
//如果到达此处,则表示XML处于有效的
//格式
if(xpp.getEventType()==XmlPullParser.START_标记)
{
if(xpp.getName().equalsIgnoreCase(“标题”))
sourceTitle=xpp.nextText();
成功=正确;
}
}
捕获(格式错误)
{
e、 printStackTrace();
Toast.makeText(getApplicationContext(),“输入RSS源的格式无效…”,Toast.LENGTH_LONG.show();
}
catch(XMLPullParseRexE)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
捕获(例外e)
{
Toast.makeText(getApplicationContext(),“发生未知错误…”,Toast.LENGTH_LONG.show();
}
返回“完成doInBackground”;
}
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
}
@凌驾
受保护的void onProgressUpdate(整数…进度)
{
super.onProgressUpdate(进度);
}
@凌驾
受保护的void onPostExecute(字符串结果)
{
super.onPostExecute(结果);
如果(成功==true)
{
db.addSource(新源(“,sourceTitle,sourceUrlString));
//刷新
Intent=getIntent();
完成();
星触觉(意向);
}
}
}
sourceUrlString
是AlertDialog中的输入字符串db
是数据库处理程序类<代码>源构造函数包含3个变量:提要的类别、标题和url,所有这些变量都是字符串
基于LogCat,每当它试图在catch
中显示Toast时,我都会收到一个错误。知道为什么吗
另外,这是我第一次尝试实现AsyncTask,因此如果您注意到任何低效的代码,请告诉我。您应该在UI线程上调用Toast。您应该在UI线程上调用Toast。
doInBackground()
方法在其单独的线程中运行。它不允许与UIThread
交互。您希望显示一条Toast消息,该消息实际上是UIThread要处理的函数
只要控件处于工作线程中,就不能更新UI。对于AsyncTask类onPreExecute()
&onProgressUpdate()
只有这两个方法允许在UI线程上执行时与UI交互。doInBackground()
方法在其单独的线程中运行。它不允许与UIThread
交互。您希望显示一条Toast消息,该消息实际上是UIThread要处理的函数
只要控件处于工作线程中,就不能更新UI。对于AsyncTask类
onPreExecute()。但是您可以从onPreExecute、onPostExecute和onProgressUpdate更新UI,因为它们在主线程上运行。因此,您可以使用这些方法来更新UI
因此,您可以发布ProgressUpdate来调用onProgressUpdate来显示toast,或者简单地向onPostexecute返回一些值,并在此基础上显示toast。由于doInBackGround在单独的线程上运行,因此您无法从该线程显示toast意味着更新UI。但是您可以从onPreExecute、onPostExecute和onProgressUpdate更新UI,因为它们在主线程上运行。因此,您可以使用这些方法来更新UI
因此,您可以publishProgress调用onProgressUpdate来显示toast,或者简单地将一些值返回到onPostexecute,并在该显示toast的基础上尝试此操作
您不能在doInbackground
中显示Toast
。所以试试下面的方法
protected String doInBackground(String... urls)
{
try
{
URL sourceUrl = new URL(sourceUrlString);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(global.getInputStream(sourceUrl), "UTF_8");
// if it reach here, it means the XML is in a valid
// format
if (xpp.getEventType() == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("title"))
sourceTitle = xpp.nextText();
succeed = true;
}
}
catch (MalformedURLException e)
{
return "MalformedURLException";
e.printStackTrace(); }
catch (XmlPullParserException e)
{
return "XmlPullParserException";
e.printStackTrace();
}
catch (IOException e)
{
return "IOException";
e.printStackTrace();
}
catch (Exception e)
{
return "Exception";
}
return "Complete doInBackground";
}
在doInBackground
中,返回如下字符串
protected String doInBackground(String... urls)
{
try
{
URL sourceUrl = new URL(sourceUrlString);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(global.getInputStream(sourceUrl), "UTF_8");
// if it reach here, it means the XML is in a valid
// format
if (xpp.getEventType() == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("title"))
sourceTitle = xpp.nextText();
succeed = true;
}
}
catch (MalformedURLException e)
{
return "MalformedURLException";
e.printStackTrace(); }
catch (XmlPullParserException e)
{
return "XmlPullParserException";
e.printStackTrace();
}
catch (IOException e)
{
return "IOException";
e.printStackTrace();
}
catch (Exception e)
{
return "Exception";
}
return "Complete doInBackground";
}
在onPostExecute
中,检查如下内容并显示Toast
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
if(result.equals("Exception"))
{
Toast.makeText(getApplicationContext(), "Unknown error occurred..", Toast.LENGTH_LONG).show();
}else if(result.equals("IOException"))
{
Toast.makeText(getApplicationContext(), "IOException..", Toast.LENGTH_LONG).show();
}else if(result.equals("XmlPullParserException"))
{
Toast.makeText(getApplicationContext(), "XmlPullParserException..", Toast.LENGTH_LONG).show();
}else if(result.equals("MalformedURLException"))
{
Toast.makeText(getApplicationContext(), "MalformedURLException..", Toast.LENGTH_LONG).show();
}else{
if(succeed)
{
db.addSource(new Sources("", sourceTitle, sourceUrlString));
//refresh
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
}
试试这个
您不能在doInbackground
中显示Toast
。所以试试下面的方法
protected String doInBackground(String... urls)
{
try
{
URL sourceUrl = new URL(sourceUrlString);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(global.getInputStream(sourceUrl), "UTF_8");
// if it reach here, it means the XML is in a valid
// format
if (xpp.getEventType() == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("title"))
sourceTitle = xpp.nextText();
succeed = true;
}
}
catch (MalformedURLException e)
{
return "MalformedURLException";
e.printStackTrace(); }
catch (XmlPullParserException e)
{
return "XmlPullParserException";
e.printStackTrace();
}
catch (IOException e)
{
return "IOException";
e.printStackTrace();
}
catch (Exception e)
{
return "Exception";
}
return "Complete doInBackground";
}
在doInBackground
中,返回如下字符串
protected String doInBackground(String... urls)
{
try
{
URL sourceUrl = new URL(sourceUrlString);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(global.getInputStream(sourceUrl), "UTF_8");
// if it reach here, it means the XML is in a valid
// format
if (xpp.getEventType() == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("title"))
sourceTitle = xpp.nextText();
succeed = true;
}
}
catch (MalformedURLException e)
{
return "MalformedURLException";
e.printStackTrace(); }
catch (XmlPullParserException e)
{
return "XmlPullParserException";
e.printStackTrace();
}
catch (IOException e)
{
return "IOException";
e.printStackTrace();
}
catch (Exception e)
{
return "Exception";
}
return "Complete doInBackground";
}
在onPostExecute
中,检查如下内容并显示Toast
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
if(result.equals("Exception"))
{
Toast.makeText(getApplicationContext(), "Unknown error occurred..", Toast.LENGTH_LONG).show();
}else if(result.equals("IOException"))
{
Toast.makeText(getApplicationContext(), "IOException..", Toast.LENGTH_LONG).show();
}else if(result.equals("XmlPullParserException"))
{
Toast.makeText(getApplicationContext(), "XmlPullParserException..", Toast.LENGTH_LONG).show();
}else if(result.equals("MalformedURLException"))
{
Toast.makeText(getApplicationContext(), "MalformedURLException..", Toast.LENGTH_LONG).show();
}else{
if(succeed)
{
db.addSource(new Sources("", sourceTitle, sourceUrlString));
//refresh
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
}
应该在UI线程上调用Toast。如果要在doBackground()中显示toast,可以使用runOnUiThread
()
应该在UI线程上调用Toast。如果要在doBackground()中显示toast,可以使用runOnUiThread
()
UI始终在单独的线程中运行,您不能从其他线程更改UI。请使用处理程序从后台线程更新UI
handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Unknown error occurred..", Toast.LENGTH_LONG).show();
}
});
UI始终在单独的线程中运行,您不能从其他线程更改UI。请使用处理程序从后台线程更新UI
handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Unknown error occurred..", Toast.LENGTH_LONG).show();
}
});
通过runOnUiThread()
实现这一点<代码>异步任务
在单独的线程中运行,因此您无法从doInBackground()
更新UI
通过
runOnUiThread()
实现这一点<代码>异步任务在单独的线程中运行,因此无法从更新UI<