我正在尝试创建一个RSS新闻阅读器应用程序,我正在使用AsyncTask来使用线程将新网站添加到RSS源列表中。但每当我输入新的URL并按“添加”时,应用程序崩溃。 LogCat说 -
threadid 12:线程退出时未捕获异常“,”AsyncTask#2 FATAL EXCEPTION“和java.Lang.RuntimeException:执行doInBackground时出错()
请帮忙! (P.S.我正在使用ADT。)
package com.rssnews;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.Exception;
public class AddNewFeed extends Activity {
Button btnSubmit;
Button btnCancel;
EditText txtUrl;
TextView textViewMessage;
RSSParser rssParser = new RSSParser();
RSSFeed rssFeed;
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnewfeed);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnCancel = (Button) findViewById(R.id.btnCancel);
txtUrl = (EditText) findViewById(R.id.txtUrl);
textViewMessage = (TextView) findViewById(R.id.textViewMessage);
btnSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
String url = txtUrl.getText().toString();
Log.d("URL Length", "" + url.length());
if (url.length() > 0) {
textViewMessage.setText("");
String urlPattern = "^http(s{0,1})://[a-zA-Z0-9_/\\-\\.]+\\.([A-Za-z/]{2,5})[a-zA-Z0-9_/\\&\\?\\=\\-\\.\\~\\%]*";
if (url.matches(urlPattern)) {
new loadRSSFeed().execute(url);
} else {
textViewMessage.setText("Please enter a valid url");
}
} else {
textViewMessage.setText("Please enter website url");
}}
catch(Exception e){
finish();
}
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
/**
* Background Async Task to get RSS data from URL
* */
class loadRSSFeed extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AddNewFeed.this);
pDialog.setMessage("Fetching RSS Information ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting
* */
@Override
protected String doInBackground(String... args) {
String url = args[0];
rssFeed = rssParser.getRSSFeed(url);
Log.d("rssFeed", " " + rssFeed);
if (rssFeed != null) {
Log.e("RSS URL",
rssFeed.getTitle() + "" + rssFeed.getLink() + ""
+ rssFeed.getDescription() + ""
+ rssFeed.getLanguage());
RSSDatabaseHandler rssDb = new RSSDatabaseHandler(
getApplicationContext());
WebSite site = new WebSite(rssFeed.getTitle(),
rssFeed.getLink(), rssFeed.getRSSLink(),
rssFeed.getDescription());
rssDb.addSite(site);
Intent i = new Intent(getApplicationContext(),
RSSNewsReaderPBActivity.class);
// send result code 100 to notify about product update
setResult(100, i);
startActivity(i);
return null;
} else {
runOnUiThread(new Runnable() {
public void run() {
textViewMessage
.setText("Rss url not found. Please check the url or try again");
}
});
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String args) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
if (rssFeed != null) {
}
}
});
}
}
@Override
protected void onPause() {
super.onPause();
//pDialog.dismiss();
finish();
}
@Override
public void onBackPressed() {
finish();
}
}
04-10 13:28:50.250: W/dalvikvm(1853): threadid=12: thread exiting with uncaught exception (group=0xb1a1bb90)
04-10 13:28:50.350: E/AndroidRuntime(1853): FATAL EXCEPTION: AsyncTask #2
04-10 13:28:50.350: E/AndroidRuntime(1853): Process: com.rssnews, PID: 1853
04-10 13:28:50.350: E/AndroidRuntime(1853): java.lang.RuntimeException: An error occured while executing doInBackground()
04-10 13:28:50.350: E/AndroidRuntime(1853): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-10 13:28:50.350: E/AndroidRuntime(1853): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-10 13:28:50.350: E/AndroidRuntime(1853): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-10 13:28:50.350: E/AndroidRuntime(1853): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-10 13:28:50.350: E/AndroidRuntime(1853): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-10 13:28:50.350: E/AndroidRuntime(1853): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-10 13:28:50.350: E/AndroidRuntime(1853): at java.lang.Thread.run(Thread.java:841)
04-10 13:28:50.350: E/AndroidRuntime(1853): Caused by: java.lang.NoClassDefFoundError: org.jsoup.Jsoup
04-10 13:28:50.350: E/AndroidRuntime(1853): at com.rssnews.RSSParser.getRSSLinkFromURL(RSSParser.java:129)
04-10 13:28:50.350: E/AndroidRuntime(1853): at com.rssnews.RSSParser.getRSSFeed(RSSParser.java:52)
04-10 13:28:50.350: E/AndroidRuntime(1853): at com.rssnews.AddNewFeed$loadRSSFeed.doInBackground(AddNewFeed.java:90)
04-10 13:28:50.350: E/AndroidRuntime(1853): at com.rssnews.AddNewFeed$loadRSSFeed.doInBackground(AddNewFeed.java:1)
04-10 13:28:50.350: E/AndroidRuntime(1853): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-10 13:28:50.350: E/AndroidRuntime(1853): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-10 13:28:50.350: E/AndroidRuntime(1853): ... 3 more
04-10 13:28:50.700: I/Choreographer(1853): Skipped 38 frames! The application may be doing too much work on its main thread.
04-10 13:28:51.880: I/Choreographer(1853): Skipped 95 frames! The application may be doing too much work on its main thread.
04-10 13:28:55.320: I/Choreographer(1853): Skipped 49 frames! The application may be doing too much work on its main thread.
04-10 13:28:56.280: I/Choreographer(1853): Skipped 96 frames! The application may be doing too much work on its main thread.
04-10 13:28:57.920: E/WindowManager(1853): android.view.WindowLeaked: Activity com.rssnews.AddNewFeed has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b2524248 V.E..... R.....I. 0,0-563,230} that was originally added here
04-10 13:28:57.920: E/WindowManager(1853): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
04-10 13:28:57.920: E/WindowManager(1853): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
04-10 13:28:57.920: E/WindowManager(1853): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-10 13:28:57.920: E/WindowManager(1853): at android.app.Dialog.show(Dialog.java:286)
04-10 13:28:57.920: E/WindowManager(1853): at com.rssnews.AddNewFeed$loadRSSFeed.onPreExecute(AddNewFeed.java:80)
04-10 13:28:57.920: E/WindowManager(1853): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
04-10 13:28:57.920: E/WindowManager(1853): at android.os.AsyncTask.execute(AsyncTask.java:535)
04-10 13:28:57.920: E/WindowManager(1853): at com.rssnews.AddNewFeed$1.onClick(AddNewFeed.java:45)
04-10 13:28:57.920: E/WindowManager(1853): at android.view.View.performClick(View.java:4424)
04-10 13:28:57.920: E/WindowManager(1853): at android.view.View$PerformClick.run(View.java:18383)
04-10 13:28:57.920: E/WindowManager(1853): at android.os.Handler.handleCallback(Handler.java:733)
04-10 13:28:57.920: E/WindowManager(1853): at android.os.Handler.dispatchMessage(Handler.java:95)
04-10 13:28:57.920: E/WindowManager(1853): at android.os.Looper.loop(Looper.java:137)
04-10 13:28:57.920: E/WindowManager(1853): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-10 13:28:57.920: E/WindowManager(1853): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 13:28:57.920: E/WindowManager(1853): at java.lang.reflect.Method.invoke(Method.java:515)
04-10 13:28:57.920: E/WindowManager(1853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-10 13:28:57.920: E/WindowManager(1853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-10 13:28:57.920: E/WindowManager(1853): at dalvik.system.NativeStart.main(Native Method)
04-10 13:28:57.980: I/Choreographer(1853): Skipped 62 frames! The application may be doing too much work on its main thread.
04-10 13:29:00.400: I/Process(1853): Sending signal. PID: 1853 SIG: 9