Android应用程序在AsyncThread中崩溃

时间:2013-12-10 22:37:00

标签: android android-asynctask

所以我的应用程序启动并适用于viewpager的第一个片段,但在加载viewpager的第二个片段时崩溃。任何帮助将非常感激。 这是源代码:

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));
         }

    }

}

这是我的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)

1 个答案:

答案 0 :(得分:0)

假设activity_previous_word.xml中包含所有文字视图,请将您的代码更改为

 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查找当前膨胀布局中提到的id的视图。因此,您需要使用膨胀的视图对象来初始化视图。