有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 studio中AsyncTask中的java nullpointerexception

我正在尝试使用Jsoup解析一个网站。 但是在onPostUpdate方法中,在更改textview时会得到nullpointerexception。 请帮我用手机运行这个。 jsoup正在分析页面,但onPostupdate无法更新TextView组件

这是我的密码

    package com.example.mayurn.sitechecker;

import 安卓.os.AsyncTask;
import 安卓.provider.DocumentsContract;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.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 安卓.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 安卓.os.AsyncTask.finish(AsyncTask.java:651)
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:     at 安卓.os.AsyncTask.access$500(AsyncTask.java:180)
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:     at 安卓.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:     at 安卓.os.Handler.dispatchMessage(Handler.java:102)
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:     at 安卓.os.Looper.loop(Looper.java:148)
12-25 16:21:09.073 12857-12857/com.example.mayurn.sitechecker W/System.err:     at 安卓.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.安卓.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.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-25 16:21:22.001 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@4a0167
12-25 16:21:22.006 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@89a2b14
12-25 16:21:22.007 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@83c9fbd
12-25 16:21:22.008 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@3a9c9b2
12-25 16:21:22.009 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@c6b4d03
12-25 16:21:22.009 12857-12857/com.example.mayurn.sitechecker V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@e78e480

共 (2) 个答案

  1. # 1 楼答案

    您正在为textview在onCreate中创建局部变量

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

    而不是

    textview = (TextView)findViewById(R.id.textView);
    
  2. # 2 楼答案

    用这条线

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

    而不是

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

    这意味着从上面的行中删除文本视图