[英]Android Application crashing with JSoup
這是我的問題的概要。 有一個帶有2個按鈕的主要活動。 當用戶單擊第一個按鈕(randomButton)時,將啟動一個新活動(Random.java)。 此新活動將具有一個TextView小部件。 TextView的文本將更改為我使用JSoup打開的網站的標題。
編輯:我正在使用JSoup 1.7.2,如果有任何區別。
我正在使用AsyncTask,但是一旦單擊按鈕以啟動新的Activity(Random.java),我的應用程序就會崩潰。
Main.java的相關部分新活動將在randomButton onClickListener內開始
randomButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Random Quote Will be Generated...", Toast.LENGTH_LONG).show(); //
//Now a new intent will be created to go to the Random.java activity!
Intent intent = new Intent(getBaseContext(), Random.class);
startActivity(intent);
}
});
Random.java
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class Random extends Activity {
//This is the activity launched when the user selects the randomButton on main activity.
Handler handler;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_random_quote);
// Show the Up button in the action bar.
setupActionBar();
handler = new Handler();
textView = (TextView)findViewById(R.id.textView);
new GetTitle().execute();
}
private class GetTitle extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
Document doc;
try {
doc = Jsoup.connect("http://www.google.com").userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").get();
return doc.title();
} catch (IOException e) {
return null;
}
}
@Override
protected void onPostExecute(String title){
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(title);
}
}
/**
* Set up the {@link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.random_quote, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
在我的AndroidManifest.xml中,我有:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
logcat的
11-20 17:07:55.452: D/ActivityThread(12564): setTargetHeapUtilization:0.25
11-20 17:07:55.452: D/ActivityThread(12564): setTargetHeapIdealFree:8388608
11-20 17:07:55.452: D/ActivityThread(12564): setTargetHeapConcurrentStart:2097152
11-20 17:07:55.752: D/dalvikvm(12564): GC_FOR_ALLOC freed 57K, 18% free 17368K/21059K, paused 25ms, total 25ms
11-20 17:07:55.823: D/dalvikvm(12564): GC_CONCURRENT freed 1K, 13% free 26468K/30215K, paused 13ms+3ms, total 29ms
11-20 17:07:56.023: I/Adreno200-EGLSUB(12564): <ConfigWindowMatch:2087>: Format RGBA_8888.
11-20 17:07:56.043: E/(12564): <s3dReadConfigFile:75>: Can't open file for reading
11-20 17:07:56.043: E/(12564): <s3dReadConfigFile:75>: Can't open file for reading
11-20 17:07:57.755: W/dalvikvm(12564): VFY: unable to resolve static method 5320: Lorg/jsoup/Jsoup;.connect (Ljava/lang/String;)Lorg/jsoup/Connection;
11-20 17:07:57.805: W/dalvikvm(12564): threadid=11: thread exiting with uncaught exception (group=0x414ce438)
11-20 17:07:57.835: I/Adreno200-EGLSUB(12564): <ConfigWindowMatch:2087>: Format RGBA_8888.
11-20 17:07:57.925: E/AndroidRuntime(12564): FATAL EXCEPTION: AsyncTask #1
11-20 17:07:57.925: E/AndroidRuntime(12564): java.lang.RuntimeException: An error occured while executing doInBackground()
11-20 17:07:57.925: E/AndroidRuntime(12564): at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-20 17:07:57.925: E/AndroidRuntime(12564): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-20 17:07:57.925: E/AndroidRuntime(12564): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-20 17:07:57.925: E/AndroidRuntime(12564): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-20 17:07:57.925: E/AndroidRuntime(12564): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-20 17:07:57.925: E/AndroidRuntime(12564): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-20 17:07:57.925: E/AndroidRuntime(12564): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-20 17:07:57.925: E/AndroidRuntime(12564): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-20 17:07:57.925: E/AndroidRuntime(12564): at java.lang.Thread.run(Thread.java:856)
11-20 17:07:57.925: E/AndroidRuntime(12564): Caused by: java.lang.NoClassDefFoundError: org.jsoup.Jsoup
11-20 17:07:57.925: E/AndroidRuntime(12564): at com.example.brainyquote.RandomQuote$GetTitle.doInBackground(RandomQuote.java:46)
11-20 17:07:57.925: E/AndroidRuntime(12564): at com.example.brainyquote.RandomQuote$GetTitle.doInBackground(RandomQuote.java:1)
11-20 17:07:57.925: E/AndroidRuntime(12564): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-20 17:07:57.925: E/AndroidRuntime(12564): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-20 17:07:57.925: E/AndroidRuntime(12564): ... 5 more
11-20 17:08:07.135: E/SpannableStringBuilder(12564): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
11-20 17:08:07.135: E/SpannableStringBuilder(12564): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
11-20 17:07:57.755: W/dalvikvm(12564): VFY: unable to resolve static method 5320: Lorg/jsoup/Jsoup;.connect (Ljava/lang/String;)Lorg/jsoup/Connection;
dalvikvm找不到Jsoup.connect(“ http://www.google.com ”)這種方法。 檢查Jsoup.jar是否在您的libs文件夾中。 如果是,請檢查是否要導出項目專用庫。
應該檢查一下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.