onPostExecute方法的NullPointerException

时间:2014-10-10 09:55:18

标签: java android android-fragments

我的应用程序的导航抽屉包含一个菜单,我可以在这些菜单之间导航。应用程序午餐后我得到的第一个片段是Home,它包含一个listView和一个连接在底部的页脚(仅用于测试)。问题是,当我第一次在家庭片段中吃午饭没有发生任何事情并且它的工作非常好,但是当我从导航抽屉中更改片段并尝试返回主片段时,应用程序崩溃。错误日志显示空指针异常。这是我的代码细节

onCreateView方法

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
       if (v != null) {
            ViewGroup parent = (ViewGroup) v.getParent();
            if (parent != null)
                parent.removeView(v);
        }
            try {


                v =   inflater.inflate(R.layout.fragment_homes, container, false);

                listView = (ListView) v.findViewById(R.id.homeList);

                l = new HomeListAdapter(this, homeList);
                if(l == null)
                    Log.i("tag","null");

                listView.setAdapter(l);
            } catch (InflateException e) {
        }

        return v;

    }

onPostExecute方法

@Override
        protected void onPostExecute(final String res) {

            /*try {
                jObj = new JSONArray(res);
            } catch (JSONException e) {
                e.printStackTrace();
            }*/

           /* JSONArray j = null;
            try {
                j = new JSONArray(res);
            } catch (JSONException e) {
                e.printStackTrace();
            }*/


            // Parsing json
             for (int i = 0; i < 5; i++) {
                 //JSONObject obj = j.getJSONObject(i);

                 Home home = new Home();
                 //String photoNews = java.net.URLDecoder.decode(obj.getString("photonews"), "UTF-8");
                 home.setNomC("Karim Ennassiri");
                 home.setPhotoP("http://i1239.photobucket.com/albums/ff509/FestusMANAFEST/iker-casillas20Wallpaper__yvt2.jpg");
                 home.setPhotoAr("http://voyage-essaouira-maroc.com/upload/apostrophe_image_(13).jpg");
                 home.setText("Chlada V2.0 Edition");
                 home.setNbrSick("5");
                 home.setNbrComm("8");
                 home.setNbrShare("0");

                 homeList.add(home);

             }

            l.notifyDataSetChanged();

            //pDialog.hide();


        }

错误日志

10-10 09:31:30.884    2447-2447/com.example.user.unchained E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user.unchained, PID: 2447
java.lang.NullPointerException
        at com.example.user.unchained.HomesActivity$PlaceholderFragment$FeedsTask.onPostExecute(HomesActivity.java:375)
        at com.example.user.unchained.HomesActivity$PlaceholderFragment$FeedsTask.onPostExecute(HomesActivity.java:277)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

这正是导致错误的行:l.notifyDataSetChanged();。我注意到错误只是在我试图显示片段时间的情况下。

所以请帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您需要在doInBackground中使用它。

                @Override
    protected Void doInBackground(void... ) {
        try {

            for (int i = 0; i < 5; i++) {
             //JSONObject obj = j.getJSONObject(i);

             Home home = new Home();
             //String photoNews = java.net.URLDecoder.decode(obj.getString("photonews"), "UTF-8");
             home.setNomC("Karim Ennassiri");
             home.setPhotoP("http://i1239.photobucket.com/albums/ff509/FestusMANAFEST/iker-casillas20Wallpaper__yvt2.jpg");
             home.setPhotoAr("http://voyage-essaouira-maroc.com/upload/apostrophe_image_(13).jpg");
             home.setText("Chlada V2.0 Edition"); //give some other name in getter setter for setText in Home.java. It makes not to understand whether you are using setText for textview assigning. 
             home.setNbrSick("5");
             home.setNbrComm("8");
             home.setNbrShare("0");

             homeList.add(home);

         }

postExecute用于显示UI。不要像在PostExecute中解析一样执行繁重的操作。