简体   繁体   English

JSoup导致Android应用程序崩溃

[英]Android Application crashing with JSoup

Here is the outline of my problem. 这是我的问题的概要。 There is a main activity with 2 buttons. 有一个带有2个按钮的主要活动。 When the user clicks on the 1st button (randomButton), a new activity (Random.java) will start. 当用户单击第一个按钮(randomButton)时,将启动一个新活动(Random.java)。 This new activity will have a TextView widget. 此新活动将具有一个TextView小部件。 The text of the TextView will change to the title of a website I pull with JSoup. TextView的文本将更改为我使用JSoup打开的网站的标题。

EDIT : I am using JSoup 1.7.2 if that makes any difference. 编辑:我正在使用JSoup 1.7.2,如果有任何区别。

I am using an AsyncTask yet my application is crashing once I click the button to start the new Activity (Random.java). 我正在使用AsyncTask,但是一旦单击按钮以启动新的Activity(Random.java),我的应用程序就会崩溃。

Relevant Portion of Main.java The new activity will start within the randomButton onClickListener 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 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);
        }

    }

Within my AndroidManifest.xml I have : 在我的AndroidManifest.xml中,我有:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

LogCat 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

Please guide me in where I have made an error. 请在出现错误的地方指导我。 Thank you. 谢谢。

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 is not finding this method Jsoup.connect(" http://www.google.com "). dalvikvm找不到Jsoup.connect(“ http://www.google.com ”)这种方法。 Check if Jsoup.jar is in your libs folder. 检查Jsoup.jar是否在您的libs文件夹中。 If yes, check if you're exporting your project private libraries. 如果是,请检查是否要导出项目专用库。

在此处输入图片说明

this should be checked. 应该检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM