-2

我想解析一个网站使用Jsoup。 但是在更改textview的onPostUpdate方法中,得到nullpointerexception。 请帮助我使用我的手机来运行此。 jsoup解析页面,但onPostupdate无法更新TextView组件。在android工作室的AsyncTask中nullpointerexception

这里是我的代码

package com.example.mayurn.sitechecker; 

import android.os.AsyncTask; 
import android.provider.DocumentsContract; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 



public class MainActivity extends AppCompatActivity { 
    TextView textview; 
    private static final String TAG="MyTag"; 
    TextView setter; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView textview = (TextView)findViewById(R.id.textView); 
     Button button = (Button)findViewById(R.id.button); 




     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       new doit().execute(); 
       Log.i(TAG,"doit executer"); 


      } 
     }); 

    } 


    public class doit extends AsyncTask<Void,Void,Void>{ 
     String words; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

     } 

     @Override 
     protected Void doInBackground(Void... params) { 

      try { 
       //Document doc = Jsoup.connect("https://en.wikipedia.org/wiki/Main_Page").get(); 
       org.jsoup.nodes.Document doc=Jsoup.connect("https://en.wikipedia.org/wiki/Main_Page").get(); 
       words = doc.text(); 
       Log.i(TAG,"Words initialized"); 
       Log.i(TAG,words); 



      } catch (Exception e) { 
       e.printStackTrace(); 
      } 


      return null; 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      try { 
       Log.i(TAG, "Inside onpostexecute"); 
       textview.setText("words"); 
      }catch (Exception e){e.printStackTrace();} 
     } 
    } 
    } 

logcat的输出

12-25 16:21:05.348 12857-12988/com.example.mayurn.sitechecker W/System.err: Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available 
12-25 16:21:09.071 12857-12988/com.example.mayurn.sitechecker I/MyTag: Words initialized 
12-25 16:21:09.072 12857-12988/com.example.mayurn.sitechecker I/MyTag: Wikipedia, the free encyclopedia Open main menu Today's featured article Hebron Church is a mid-19th century Lutheran church in Intermont, Hampshire County, in the U.S. state of West Virginia. It was founded in 1786 as Great Capon Church by German settlers in the Cacapon River Valley, making it the first Lutheran church west of the Shenandoah Valley. The congregation worshiped in a log church, which initially served both Lutheran and Reformed denominations. In 1821, records and sermons transitioned from German to English. The church's congregation built the present Greek Revival-style church building in 1849, when it was renamed Hebron on the Cacapon. The original log church was moved across the road and used as a sexton's house, Sunday school classroom, and public schoolhouse. To celebrate the congregation's 175th anniversary in 1961, Hebron Church constructed a building for community functions and religious education, designed to be architecturally compatible with the 1849 brick church. Hebron Church was listed on the National Register of Historic Places in 2014, cited as a Potomac Highlands church with vernacular Greek Revival architecture. (Full article...) Recently featured: Themes in Maya Angelou's autobiographies Richard Dannatt Andrew Sledd Archive By email More featured articles... In the news Breitscheidplatz Christmas market The United Nations Security Council adopts a resolution condemning Israeli settlements in the West Bank. Syrian government forces retake control of the besieged areas of Aleppo. More than 30 people are killed by government security forces as protests break out across the Democratic Republic of the Congo following Joseph Kabila's refusal to step down after the completion of his scheduled term in office. An explosion at a fireworks market in Tultepec, Mexico, kills at least 32 people and injures more than 50 others. A terrorist attack kills 12 people and injures 56 others, as a stolen truck is rammed into a Christmas market (pictured) in Berlin, Germany. Ongoing: Battle of Mosul Recent deaths: Rick Parfitt Franca Sozzani Corno Michèle Morgan Retrieved from "https://en.wikipedia.org/w/index.php?title=Main_Page&oldid=749836961" Read in another language View edit history of this page. ® Content is available under CC BY-SA 3.0 unless otherwise noted. Terms of Use Privacy Desktop 
12-25 16:21:09.072 12857-12857/com.example.mayurn.sitechecker I/MyTag: Inside onpostexecute 
12-25 16:21:09.072 12857-12857/com.example.mayurn.sitechecker W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 
12-25 16:21:09.072 12857-12857/com.example.mayurn.sitechecker W/System.err:  at com.example.mayurn.sitechecker.MainActivity$doit.onPostExecute(MainActivity.java:82) 
12-25 16:21:09.072 12857-12857/com.example.mayurn.sitechecker W/System.err:  at com.example.mayurn.sitechecker.MainActivity$doit.onPostExecute(MainActivity.java:49) 
12-25 16:21:09.072 12857-12857/com.example.mayurn.sitechecker W/System.err:  at android.os.AsyncTask.finish(AsyncTask.java:651) 
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:  at android.os.AsyncTask.access$500(AsyncTask.java:180) 
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:102) 
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:  at android.os.Looper.loop(Looper.java:148) 
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5451) 
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
12-25 16:21:22.001 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = [email protected] 
12-25 16:21:22.006 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = [email protected] 
12-25 16:21:22.007 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = [email protected] 
12-25 16:21:22.008 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = [email protected] 
12-25 16:21:22.009 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = [email protected] 
12-25 16:21:22.009 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = [email protected] 
+0

你再次为textview创建对象oncreate [TextView textview =(TextView)findViewById(R.id.textView);]移除第一个textView – Nas

回答

1

您在onCreate为TextView中创建局部变量,

TextView textview = (TextView)findViewById(R.id.textView); 

,而不是,

textview = (TextView)findViewById(R.id.textView); 
0

使用此线

textview = (TextView)findViewById(R.id.textView); 

代替

TextView textview = (TextView)findViewById(R.id.textView); 

大公意味着从线中删除上述的TextView。