Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取接口上的空指针异常_Android_Interface_Android Asynctask - Fatal编程技术网

Android 获取接口上的空指针异常

Android 获取接口上的空指针异常,android,interface,android-asynctask,Android,Interface,Android Asynctask,我想把Arraylist从一个类传递给扩展ListFragments的类,但无论何时调用它,我都会得到null指针异常 异步类 public class SearchJobAsync extends AsyncTask<String, String, String> { private String response; Context c; SearchModel data; ArrayList<SearchModel> values;

我想把Arraylist从一个类传递给扩展ListFragments的类,但无论何时调用它,我都会得到null指针异常

异步类

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


}

使用此异步的其他活动

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 ();
    }
}
公共类SearchJobsFragment扩展片段实现View.OnClickListener{
私人视野;
私人纺纱机类别;
私人按钮搜索;
私人纺纱机;
私人纺纱机,每月;
私人纺纱机;
私人纺纱机;
私有编辑文本和工作代码;
私有字符串[]yrsArray,monthArray,professionArray;
私人阵列适用于toYrs、toMonths、profession;
私有上下文c;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.search\u jobs\u frag,container,false);
返回视图;
}
私有void初始化(){
c=getActivity();
spCategory=(微调器)getActivity().findViewById(R.id.sp_category);
btSearch=(按钮)getActivity().findViewById(R.id.bt_search);
spToYrs=(微调器)getActivity().findViewById(R.id.sp_至_yrs);
spFromMonths=(微调器)getActivity().findViewById(R.id.sp\u from\u months);
spFromYrs=(微调器)getActivity().findViewById(R.id.sp_from_yrs);
spToMonths=(微调器)getActivity().findViewById(R.id.sp_到_个月);
etJobCode=(EditText)getActivity().findViewById(R.id.et_作业代码);
btSearch.setOnClickListener(此);
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
初始化();
喷丝头装置();
}
专用void喷丝头设置(){
//往返年
yrsArray=getResources().getStringArray(R.array.Experience\u yrss);
toYrs=新阵列适配器(c,R.layout.spinner_textview,yrsArray);
toYrs.setDropDownViewResource(android.R.layout.simple\u list\u item\u single\u choice);
spToYrs.setAdapter(toYrs);
spFromYrs.setAdapter(toYrs);
//往返月份
monthArray=getResources().getStringArray(R.array.Experience\u个月);
toMonths=新的阵列适配器(蒙塔雷的c、R.layout.spinner_textview);
toMonths.setDropDownViewResource(android.R.layout.simple\u list\u item\u single\u choice);
spToMonths.setAdapter(toMonths);
spFromMonths.setAdapter(明天);
//类别
professionArray=getResources().getStringArray(R.array.Profession);
Arrays.sort(professionArray,1,professionArray.length);
profession=新阵列适配器(c,R.layout.spinner\U textview,professionArray);
profession.setDropDownViewResource(android.R.layout.simple\u list\u item\u single\u choice);
spCategory.setAdapter(专业);
}
@凌驾
公共void onClick(视图){
SearchJobAsync SearchJobAsync=新的SearchJobAsync(c);
searchJobAsync.execute();
}
}

尝试交换此行

searchJobAsync.execute ();
searchJobAsync = new SearchJobAsync (c, data, this);
更新后的问题: 我确信SearchJobsFragment会导致错误。 原因有二:

你不能跑

searchJobAsync.execute ();
searchJobAsync = new SearchJobAsync (c, data, this);
按此顺序,它会导致execute方法中出现空指针异常。所以,我认为SearchJobList类从未在您的项目中使用过

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


您需要从onClick方法调用SearchJobAsync(上下文、ArrayList、getArrayList)构造函数。否则,SearchJobAsync中的列表变量将保持null并导致异常。

问题:

implements SearchJobAsync.getArrayList
if (values.size () > 0) {
            if(list != null)
                list.getList (values);
            }
您正试图调用另一个非静态类上的接口,因此编译器在找到它时遇到了一些问题

解决方案:

implements SearchJobAsync.getArrayList
if (values.size () > 0) {
            if(list != null)
                list.getList (values);
            }
使您的
getArrayList
成为
静态界面

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

    }

主席先生,错误与我刚才所说的相同above@Anuj是的,你提到了,但我们需要日志,所以我们知道什么是problem@Anuj您是在某些类中有多个SearchJobAsync,还是仅在ListFragment中有多个?我在其他类中调用了SearchJobAsync.executeActivity@Anuj我建议把你的界面作为一个单独的类,请发布代码其他名为SearchJobAsync的活动
static interface getArrayList {
        public void getList(ArrayList<SearchModel> data);

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