简体   繁体   English

Android应用程序在AsyncThread中崩溃

[英]Android application crashing in AsyncThread

So my application starts up and works for the first fragment of the viewpager but then crashes when loading the second fragment of the viewpager. 因此,我的应用程序启动并针对viewpager的第一个片段工作,但是在加载viewpager的第二个片段时崩溃。 Any help would be much appreciated. 任何帮助将非常感激。 Here is the source code: 这是源代码:

public class PreviousWord extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
    View v = (inflater.inflate(R.layout.activity_previous_word, container, false)); 
    new GetWord().execute();
    return v;
}

private class GetWord extends AsyncTask<Void,Void, Void>{
       private Element data=null;
       private Element data2=null;
       private Element date=null;
       private String wordName;
       private String dates;
       private TextView date1;
       private TextView date2;
       private TextView date3;
       private TextView date4;
       private TextView date5;
       private TextView date6;
       private ArrayList<String> wordList = new ArrayList<String>();
       private ArrayList<String> dateList = new ArrayList<String>();


       @Override
       protected Void doInBackground(Void... arg0) { 
            Document doc = null;
            try {
                doc = Jsoup.connect("http://www.urbandictionary.com/").get();
                for(int i = 1; i<7; i=i+1){

                    data = doc.getElementsByClass("word").get(i);
                    data2 = data.select("a[href]").first();
                    wordName = data2.text();
                    wordList.add(wordName);

                    date = doc.getElementsByClass("smallcaps").get(i);
                    dates = date.text();
                    dateList.add(dates);

                    date1 = (TextView) DailyWord.v.findViewById(R.id.text1);
                    date2 = (TextView) DailyWord.v.findViewById(R.id.text3);
                    date3 = (TextView) DailyWord.v.findViewById(R.id.text5);
                    date4 = (TextView) DailyWord.v.findViewById(R.id.text7);
                    date5 = (TextView) DailyWord.v.findViewById(R.id.text9);
                    date6 = (TextView) DailyWord.v.findViewById(R.id.text11);

                }

            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {

            date1.setText(dateList.get(0));
            date2.setText(dateList.get(1));
            date3.setText(dateList.get(2));
            date4.setText(dateList.get(3));
            date5.setText(dateList.get(4));
            date6.setText(dateList.get(5));
         }

    }

}

Here is my LogCat: 这是我的LogCat:

12-10 22:25:50.055: W/dalvikvm(19935): threadid=1: thread exiting with uncaught exception (group=0x40fbb2a0)
12-10 22:25:50.065: E/AndroidRuntime(19935): FATAL EXCEPTION: main
12-10 22:25:50.065: E/AndroidRuntime(19935): java.lang.NullPointerException
12-10 22:25:50.065: E/AndroidRuntime(19935):    at com.dictionary.urbanword.PreviousWord$GetWord.onPostExecute(PreviousWord.java:75)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at com.dictionary.urbanword.PreviousWord$GetWord.onPostExecute(PreviousWord.java:1)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at android.os.AsyncTask.finish(AsyncTask.java:631)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at android.os.Looper.loop(Looper.java:137)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at android.app.ActivityThread.main(ActivityThread.java:4898)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at java.lang.reflect.Method.invoke(Method.java:511)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-10 22:25:50.065: E/AndroidRuntime(19935):    at dalvik.system.NativeStart.main(Native Method)

Assuming activity_previous_word.xml has all the textviews in it change your code to 假设activity_previous_word.xml包含所有textview,则将代码更改为

 public class PreviousWord extends Fragment {
     private Element data=null;
       private Element data2=null;
       private Element date=null;
       private String wordName,dates;
       private TextView date1,date2,date3,date4,date5,date6;
       private ArrayList<String> wordList = new ArrayList<String>();
       private ArrayList<String> dateList = new ArrayList<String>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
    View v = (inflater.inflate(R.layout.activity_previous_word, container, false)); 

                    date1 = (TextView) v.findViewById(R.id.text1);
                    date2 = (TextView) v.findViewById(R.id.text3);
                    date3 = (TextView) v.findViewById(R.id.text5);
                    date4 = (TextView) v.findViewById(R.id.text7);
                    date5 = (TextView) v.findViewById(R.id.text9);
                    date6 = (TextView) v.findViewById(R.id.text11);  
    new GetWord().execute();
    return v;
}

private class GetWord extends AsyncTask<Void,Void, Void>{



       @Override
       protected Void doInBackground(Void... arg0) { 
            Document doc = null;
            try {
                doc = Jsoup.connect("http://www.urbandictionary.com/").get();
                for(int i = 1; i<7; i=i+1){

                    data = doc.getElementsByClass("word").get(i);
                    data2 = data.select("a[href]").first();
                    wordName = data2.text();
                    wordList.add(wordName);

                    date = doc.getElementsByClass("smallcaps").get(i);
                    dates = date.text();
                    dateList.add(dates);

                }

            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {

            date1.setText(dateList.get(0));
            date2.setText(dateList.get(1));
            date3.setText(dateList.get(2));
            date4.setText(dateList.get(3));
            date5.setText(dateList.get(4));
            date6.setText(dateList.get(5));
         }

    }

}

findViewById looks for a view with the id mentioned in the current inflated layout. findViewById查找具有当前膨胀布局中提到的ID的视图。 So you need to use the inflated view object to initialize your views. 因此,您需要使用膨胀的视图对象来初始化视图。

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

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