gpt4 book ai didi

java - Android 应用程序因 JSoup 而崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 08:54:25 24 4
gpt4 key购买 nike

这是我的问题的概要。有一个带有 2 个按钮的主要 Activity 。当用户单击第一个按钮 (randomButton) 时,将启动一个新 Activity (Random.java)。这个新 Activity 将有一个 TextView 小部件。 TextView 的文本将更改为我使用 JSoup 拉取的网站的标题。

编辑: 如果有任何不同,我正在使用 JSoup 1.7.2。

我正在使用 AsyncTask,但一旦我单击按钮启动新 Activity (Random.java),我的应用程序就会崩溃。

Main.java 的相关部分新 Activity 将在 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);

}
});

随机.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 文件夹中。如果是,请检查您是否正在导出项目私有(private)库。

enter image description here

这应该被检查。

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

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