onPostExecute中的NullPointerException

时间:2014-06-02 19:49:23

标签: java android algorithm

/**
 * A placeholder fragment containing a simple view.
 */
public class FragmentOS extends Fragment{

    private Dialog dialog;


    public FragmentOS() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_os, container, false);
        return rootView;
    }


    @Override 
    public void onActivityCreated(Bundle savedInstanceState) {  
        super.onActivityCreated(savedInstanceState);

        new ListaOS1(getActivity().getApplicationContext()).execute();

    }



    public class ListaOS1 extends AsyncTask<Void, Void, ArrayList<OS>>{

        private Context context;
        private LinearLayout linlaHeaderProgress;
        private ArrayList<OS> array;

        public ListaOS1(Context context)
        {
            this.context = context;
        }


        @Override
        protected ArrayList<OS> doInBackground(Void... params) {

            array = new ArrayList<OS>();
            array = new OSController().listarTodos();



            return array;
        }

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

            linlaHeaderProgress = (LinearLayout) getActivity().findViewById(R.id.linlaHeaderProgress);

            linlaHeaderProgress.setVisibility(View.VISIBLE);

        }

        protected void onPostExecute(ArrayList<OS> result) {
            super.onPostExecute(result);

            if(array.size() > 0){
                BaseAdapterOS ad = new BaseAdapterOS(getActivity(),array);
                ListView lv = (ListView) getActivity().findViewById(R.id.listViewOS);
                lv.setAdapter(ad);
            }

            if(linlaHeaderProgress.isShown() == true)
                linlaHeaderProgress.setVisibility(View.GONE);
        }

    }

     @Override
        public void onResume(){
            super.onResume();

            new ListaOS1(getActivity().getApplicationContext()).execute();

        }



}

错误:

06-02 19:39:38.770: E/AndroidRuntime(28279): FATAL EXCEPTION: main
06-02 19:39:38.770: E/AndroidRuntime(28279): java.lang.NullPointerException
06-02 19:39:38.770: E/AndroidRuntime(28279):    at com.bioscontrole2.FragmentOS$ListaOS1.onPostExecute(FragmentOS.java:102)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at com.bioscontrole2.FragmentOS$ListaOS1.onPostExecute(FragmentOS.java:1)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at android.os.AsyncTask.finish(AsyncTask.java:631)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at android.os.Looper.loop(Looper.java:137)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at android.app.ActivityThread.main(ActivityThread.java:4929)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at java.lang.reflect.Method.invokeNative(Native Method)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at java.lang.reflect.Method.invoke(Method.java:511)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
06-02 19:39:38.770: E/AndroidRuntime(28279):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

可能你应该在onPostExecute中使用if(result.size()&gt; 0)而不是if(array.size()&gt; 0)。

答案 1 :(得分:0)

您是否在onPostExecute中设置了断点?是数组实例化的吗?