在接口上获取空指针异常

时间:2014-06-26 07:21:33

标签: android interface android-asynctask

我想把类中的Arraylist传递给扩展ListFragments的类。但每当我调用它时,我都会得到空指针异常。

异步类

public class SearchJobAsync extends AsyncTask<String, String, String> {
    private String response;
    Context c;
    SearchModel data;
    ArrayList<SearchModel> values;
    getArrayList list;


    public interface getArrayList {
        public void getList(ArrayList<SearchModel> data);

    }

    public SearchJobAsync(Context c, ArrayList<SearchModel> values, getArrayList list) {
        this.c = c;
        this.values = values;
        this.list = list;
    }

    public SearchJobAsync(Context c) {
        this.c = c;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute ();
        CommonFunctions.showProgress (c, "Please Wait...", true);

    }

    @Override
    protected void onPostExecute(String s) {
        values = new ArrayList<SearchModel> ();

        super.onPostExecute (s);
        if (!s.trim ().contains ("Table")) {
            Crouton.makeText ((android.app.Activity) c, "Nothing found", Style.INFO).show ();
        } else {
            try {

                JSONObject jsonObject = new JSONObject (s);
                JSONObject NewDataSet = jsonObject.getJSONObject ("NewDataSet");
                if (NewDataSet.get ("Table") instanceof JSONObject) {
                    JSONObject table = NewDataSet.getJSONObject ("Table");
                    data = new SearchModel (table.getString ("Job_Category"), table.getString ("Min_Exp"), table.getString ("Max_Exp"), table.getString ("Posted_On"), table.getString ("Candidate_Counts"), table.getString ("Applications"), table.getString ("No_Of_Pos"), table.getString ("Job_Desc"), table.getString ("Job_Type"), table.getString ("Job_Hours"), table.getString ("Job_Status"), table.getString ("Job_Exp_Date"), table.getString ("Address"), table.getString ("Gender_Name"), table.getString ("Religion_Name"), table.getString ("Exp_Summary"), table.getString ("IJob_Request_ID"), table.getString ("Requestor_Name"));
                    values.add (data);
                } else if (NewDataSet.get ("Table") instanceof JSONArray) {
                    JSONArray tableArray = NewDataSet.getJSONArray ("Table");

                    for (int i = 0; i < tableArray.length (); i++) {
                        JSONObject table = tableArray.getJSONObject (i);
                        data = new SearchModel (table.getString ("Job_Category"), table.getString ("Min_Exp"), table.getString ("Max_Exp"), table.getString ("Posted_On"), table.getString ("Candidate_Counts"), table.getString ("Applications"), table.getString ("No_Of_Pos"), table.getString ("Job_Desc"), table.getString ("Job_Type"), table.getString ("Job_Hours"), table.getString ("Job_Status"), table.getString ("Job_Exp_Date"), table.getString ("Address"), table.getString ("Gender_Name"), table.getString ("Religion_Name"), table.getString ("Exp_Summary"), table.getString ("IJob_Request_ID"), table.getString ("Requestor_Name"));
                        values.add (data);

                    }

                }
                if (values.size () > 0) {
                    list.getList (values);
                }

            } catch (JSONException e) {
                e.printStackTrace ();
            }
        }


        CommonFunctions.showProgress (c, "", false);
        Intent i = new Intent (c, SearchJobListActivity.class);
        c.startActivity (i);
    }

    @Override
    protected String doInBackground(String... s) {
        response = HttpRequest.post ("https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/FindJobForVendor").send ("Vendor_IEntity_Code=" + "34588A34-E969-4723-84FE-E5409B66A5B7" + "&Job_Code=" + "&Job_Category=1" + "&Exp_Years_From=0" + "&Exp_Months_From=0" + "&Exp_Years_To=0" + "&Exp_Months_To=0").body ();
        response = response.replaceAll ("<[^>]*>", "").replaceAll ("\n", "");
        Log.e ("Search Jobs", "" + response);
        return response;
    }


}

ListFragment类

public class SearchJobList extends ListFragment implements SearchJobAsync.getArrayList {
    private View view;
    private ListView lvSearchJobs;
    private ArrayList<SearchModel> data;
    SearchJobCustomList customList;
    SearchJobAsync searchJobAsync;
    private Context c;

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated (savedInstanceState);
        c = getActivity ();
        lvSearchJobs = (ListView) getActivity ().findViewById (android.R.id.list);
        data = new ArrayList<SearchModel> ();
        searchJobAsync.execute ();
        searchJobAsync = new SearchJobAsync (c, data, this);


    }

    @Override
    public void getList(ArrayList<SearchModel> data) {
        customList = new SearchJobCustomList (c, data);
        setListAdapter (customList);
    }
}

我在logcat

所说的这行上遇到错误
list.getList (values);

public class SearchJobAsync extends AsyncTask<String, String, String>

logcat的

06-26 13:03:46.256  31713-31713/com.jobs_on_call_fragments E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.jobs_on_call_fragments.AsyncTasks.SearchJobAsync.onPostExecute(SearchJobAsync.java:83)
            at com.jobs_on_call_fragments.AsyncTasks.SearchJobAsync.onPostExecute(SearchJobAsync.java:25)
            at android.os.AsyncTask.finish(AsyncTask.java:631)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

使用此Async的其他活动

public class SearchJobsFragment extends Fragment implements View.OnClickListener {

    private View view;
    private Spinner spCategory;
    private Button btSearch;
    private Spinner spToYrs;
    private Spinner spFromMonths;
    private Spinner spFromYrs;
    private Spinner spToMonths;
    private EditText etJobCode;
    private String[] yrsArray, monthArray, professionArray;
    private ArrayAdapter<String> toYrs, toMonths, profession;
    private Context c;

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

    private void initialize() {

        c = getActivity ();
        spCategory = (Spinner) getActivity ().findViewById (R.id.sp_category);
        btSearch = (Button) getActivity ().findViewById (R.id.bt_search);
        spToYrs = (Spinner) getActivity ().findViewById (R.id.sp_to_yrs);
        spFromMonths = (Spinner) getActivity ().findViewById (R.id.sp_from_months);
        spFromYrs = (Spinner) getActivity ().findViewById (R.id.sp_from_yrs);
        spToMonths = (Spinner) getActivity ().findViewById (R.id.sp_to_months);
        etJobCode = (EditText) getActivity ().findViewById (R.id.et_job_code);
        btSearch.setOnClickListener (this);
    }

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

    private void spinnerSetup() {

        //for to/from yrs

        yrsArray = getResources ().getStringArray (R.array.Experience_yrss);
        toYrs = new ArrayAdapter<String> (c, R.layout.spinner_textview, yrsArray);
        toYrs.setDropDownViewResource (android.R.layout.simple_list_item_single_choice);
        spToYrs.setAdapter (toYrs);
        spFromYrs.setAdapter (toYrs);


        // for to/from months
        monthArray = getResources ().getStringArray (R.array.Experience_months);
        toMonths = new ArrayAdapter<String> (c, R.layout.spinner_textview, monthArray);
        toMonths.setDropDownViewResource (android.R.layout.simple_list_item_single_choice);
        spToMonths.setAdapter (toMonths);
        spFromMonths.setAdapter (toMonths);


        //for category

        professionArray = getResources ().getStringArray (R.array.Profession);
        Arrays.sort (professionArray, 1, professionArray.length);
        profession = new ArrayAdapter<String> (c, R.layout.spinner_textview, professionArray);
        profession.setDropDownViewResource (android.R.layout.simple_list_item_single_choice);
        spCategory.setAdapter (profession);


    }

    @Override
    public void onClick(View view) {
        SearchJobAsync searchJobAsync = new SearchJobAsync (c);
        searchJobAsync.execute ();
    }
}

2 个答案:

答案 0 :(得分:0)

尝试交换这一行。

searchJobAsync.execute ();
searchJobAsync = new SearchJobAsync (c, data, this);

更新后的问题:     我确定SearchJobsFragment会导致错误。 有两个原因:

1你无法运行

searchJobAsync.execute ();
searchJobAsync = new SearchJobAsync (c, data, this);

按此顺序,它会在execute方法中导致空指针异常。所以,我认为SearchJobList类从未在你的项目中使用过。

2我使用具有类似行为的SearchJobAsync和ListFragment类构建小型测试项目,并且它工作正常。所以,SearchJobsFragment中存在问题。

您需要从onClick方法调用SearchJobAsync(Context,ArrayList&lt;&gt;,getArrayList)构造函数。否则,SearchJobAsync中的列表变量将保持为空并导致异常。

答案 1 :(得分:0)

<强>问题:

implements SearchJobAsync.getArrayList

你试图在一个非静态的不同类上调用一个接口,这就是为什么编译器遇到麻烦的原因

<强>溶液

getArrayList成为static interface

static interface getArrayList {
        public void getList(ArrayList<SearchModel> data);

    }

编辑:

如果您从其他活动中调用它,则调用空指针检查

<强>样品:

if (values.size () > 0) {
            if(list != null)
                list.getList (values);
            }