简体   繁体   中英

Android Application crashing with JSoup

Here is the outline of my problem. There is a main activity with 2 buttons. When the user clicks on the 1st button (randomButton), a new activity (Random.java) will start. This new activity will have a TextView widget. The text of the TextView will change to the title of a website I pull with JSoup.

EDIT : I am using JSoup 1.7.2 if that makes any difference.

I am using an AsyncTask yet my application is crashing once I click the button to start the new Activity (Random.java).

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

    }

Within my AndroidManifest.xml I have :

<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

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 "). Check if Jsoup.jar is in your libs folder. If yes, check if you're exporting your project private libraries.

在此处输入图片说明

this should be checked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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