以下是我的问题的概要。有一个主要活动有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
答案 0 :(得分:3)
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文件夹中。如果是,请检查您是否导出项目私有库。
应该检查。