【问题标题】:Getting null while using interface method使用接口方法时获取null
【发布时间】:2014-06-27 11:54:33
【问题描述】:

我创建了一个执行所有后台任务的异步任务。在该异步任务类中,我使用方法完成创建了一个接口。它用于让我知道后台任务已经结束。但是当我使用那个界面我把它弄成空了

异步类

 public static class SearchJobsAsync extends AsyncTask<String, String, String> {
            String response;
            boolean value;
            Context c;
            finished finished;

            public SearchJobsAsync(Context c, SearchJobsAsync.finished finished,boolean value) {
                this.c = c;
                this.finished = finished;
                this.value=value;

            }

            public static interface finished {
                void Finish(boolean finish);
            }

            SearchJobsAsync(Context c, boolean value) {
                this.value = value;
                this.c = c;


            }

            @Override
            protected String doInBackground(String... strings) {
                if (spYears.getSelectedItemPosition () == 0 && spMonths.getSelectedItemPosition () == 0 && spToYears.getSelectedItemPosition () == 0 && spToMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=0" + "&Exp_Months_From=0" + "&Exp_Years_To=99" + "&Exp_Months_To=99").body ();
                } else if (spYears.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=0" + "&Exp_Months_From=" + strMnths + "&Exp_Years_To=" + strToYrs + "&Exp_Months_To=" + strToMnths).body ();
                } else if (spMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=" + strYrs + "&Exp_Months_From=0" + "&Exp_Years_To=" + strToYrs + "&Exp_Months_To=" + strToMnths).body ();
                } else if (spYears.getSelectedItemPosition () == 0 && spMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=0" + "&Exp_Months_From=0" + "&Exp_Years_To=" + strToYrs + "&Exp_Months_To=" + strToMnths).body ();
                } else if (spToYears.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=" + strYrs + "&Exp_Months_From=" + strMnths + "&Exp_Years_To=25" + "&Exp_Months_To=" + strToMnths).body ();
                } else if (spToMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=" + strYrs + "&Exp_Months_From=" + strMnths + "&Exp_Years_To=" + strToYrs + "&Exp_Months_To=25").body ();
                } else if (spToYears.getSelectedItemPosition () == 0 && spToMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=" + strYrs + "&Exp_Months_From=" + strMnths + "&Exp_Years_To=25" + "&Exp_Months_To=25").body ();
                } else if (spYears.getSelectedItemPosition () == 0 && spMonths.getSelectedItemPosition () == 0 && spToYears.getSelectedItemPosition () == 0 && spToMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=0" + "&Exp_Months_From=0" + "&Exp_Years_To=25" + "&Exp_Months_To=25").body ();
                } else {
                    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=" + strCategory + "&Exp_Years_From=" + strYrs + "&Exp_Months_From=" + strMnths + "&Exp_Years_To=" + strToYrs + "&Exp_Months_To=" + strToMnths).body ();

                }
                //Vendor_IEntity_Code=string&Job_Code=string&Job_Category=string&Exp_Years_From=string&Exp_Months_From=string&Exp_Years_To=string&Exp_Months_To=string
                response = response.replaceAll ("<[^>]*>", "").replaceAll ("\n", "");
                Log.e (" Search Job Response", "" + response);
                return response;

            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute ();
    //            CommonFunctions cs = new CommonFunctions ();
    //            cs.showProgressBar (c, true);
                pDialog = new ProgressDialog (c);
                pDialog.setMessage ("Please wait...");
                pDialog.setCancelable (false);
                pDialog.show ();
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute (s);
                arraylist = new ArrayList<HashMap<String, String>> ();
                try {
                    jsonObject = new JSONObject (s);
                    NewDataSet = jsonObject.getJSONObject ("NewDataSet");
                    if (NewDataSet == null) {
                        Toast.makeText (c, "error", Toast.LENGTH_SHORT).show ();
                    } else if (NewDataSet.get ("Table") instanceof JSONObject) {
                        HashMap<String, String> map = new HashMap<String, String> ();
                        JSONObject table = NewDataSet.getJSONObject ("Table");

                        map.put ("category", table.getString ("Job_Category"));
                        map.put ("minExp", table.getString ("Min_Exp"));
                        map.put ("maxExp", table.getString ("Max_Exp"));
                        map.put ("postedOn", table.getString ("Posted_On"));
                        map.put ("counts", table.getString ("Candidate_Counts"));
                        map.put ("applied", table.getString ("Applications"));
                        map.put ("position", table.getString ("No_Of_Pos"));
                        map.put ("desc", table.getString ("Job_Desc"));
                        map.put ("type", table.getString ("Job_Type"));
                        map.put ("hours", table.getString ("Job_Hours"));
                        map.put ("status", table.getString ("Job_Status"));
                        map.put ("expiryDate", table.getString ("Job_Exp_Date"));
                        map.put ("address", table.getString ("Address"));
                        map.put ("state", table.getString ("Job_State"));
                        map.put ("country", table.getString ("Job_Country"));
                        map.put ("city", table.getString ("Job_City"));
                        map.put ("gender", table.getString ("Gender_Name"));
                        map.put ("religion", table.getString ("Religion_Name"));
                        map.put ("summary", table.getString ("Exp_Summary"));
                        map.put ("requestId", table.getString ("IJob_Request_ID"));
                        map.put ("requestorName", table.getString ("Requestor_Name"));
    //                    map.put ("requestId",table.getString ("IJob_Request_ID"));


                        arraylist.add (map);


                    } else if (NewDataSet.get ("Table") instanceof JSONArray) {

                        JSONArray tablearray = NewDataSet.getJSONArray ("Table");

                        for (int i = 0; i < tablearray.length (); i++) {
                            HashMap<String, String> map = new HashMap<String, String> ();

                            JSONObject table = tablearray.getJSONObject (i);

                            map.put ("code", table.getString ("Job_Code"));
                            map.put ("category", table.getString ("Job_Category"));
                            map.put ("minExp", table.getString ("Min_Exp"));
                            map.put ("maxExp", table.getString ("Max_Exp"));
                            map.put ("postedOn", table.getString ("Posted_On"));
                            map.put ("counts", table.getString ("Candidate_Counts"));
                            map.put ("applied", table.getString ("Applications"));
                            map.put ("position", table.getString ("No_Of_Pos"));
                            map.put ("desc", table.getString ("Job_Desc"));
                            map.put ("type", table.getString ("Job_Type"));
                            map.put ("hours", table.getString ("Job_Hours"));
                            map.put ("status", table.getString ("Job_Status"));
                            map.put ("expiryDate", table.getString ("Job_Exp_Date"));
                            map.put ("address", table.getString ("Address"));
                            map.put ("state", table.getString ("Job_State"));
                            map.put ("country", table.getString ("Job_Country"));
                            map.put ("city", table.getString ("Job_City"));
                            map.put ("gender", table.getString ("Gender_Name"));
                            map.put ("religion", table.getString ("Religion_Name"));
                            map.put ("summary", table.getString ("Exp_Summary"));
                            map.put ("requestId", table.getString ("IJob_Request_ID"));
                            map.put ("requestorName", table.getString ("Requestor_Name"));


                            arraylist.add (map);
    //                        add.add (values);


                        }


                    }
                } catch (JSONException e) {
                    e.printStackTrace ();
                }
                if (value) {
                    finished.Finish (true);
                    Intent i = new Intent (c, SearchJobsList.class);
                    c.startActivity (i);
                }
                if (pDialog.isShowing ()) {
                    pDialog.dismiss ();
                }

            }
        }

        @Override
        public void onBackPressed() {
            super.onBackPressed ();
            Intent i = new Intent (c, Home.class);
            startActivity (i);
        }
    }

LOGCAT

06-27 17:14:30.406  11554-11554/com.example.jobs_on_call.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.jobs_on_call_search_jobs.SearchJobs$SearchJobsAsync.onPostExecute(SearchJobs.java:334)
            at com.example.jobs_on_call_search_jobs.SearchJobs$SearchJobsAsync.onPostExecute(SearchJobs.java:160)
            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)

【问题讨论】:

  • 这里的代码太多。尝试调试您的代码,如果有不明白的地方,请提出一个准确的问题。
  • 我在............完成。完成(真);
  • 你能发布完整的堆栈跟踪吗?
  • 问题出在你的构造函数 man.just 用当前参数调用单个构造函数。
  • 意味着我应该使用单个构造函数???

标签: java android interface android-asynctask


【解决方案1】:

// 试试下面的代码。只需创建一个构造函数。

//接口代码为:

 public interface WebServiceListener {

 // what ever you want to use arraylist or map.
 public void onActionComplete (ArrayList<String> _arraList);     
 public void onActionCompletes (HashMap<String, String> _map);   
 }

//你的异步类

 public class SearchJobsAsync extends AsyncTask<String, String, String> {

    WebServiceListener _object = null;
    Context _CTX;


  public SearchJobsAsync(Context _ctx,WebServiceListener object) {
    this._CTX = _ctx;
    this._object = object;
}

@Override
protected String doInBackground(String... params) {
    String _data = null;
    try {
        // do your stuff
    } catch (Exception e) {
        e.printStackTrace();
    }

    return _data;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);    
    HashMap<String, String> map = new HashMap<String,String>();
    map.put("KEY", result);
    object.onActionCompletes(map);
    _arraylist.add(result);
    object.onActionComplete(_arraylist);
}

}

// 现在在您想要数据的主要活动中,只需创建 Async 类的对象并传递如下参数:

  SearchJobsAsync   _parse = new SearchJobsAsync(_ctx,this);

// 现在实现接口的方法并获取如下数据:

@Override
public void onActionComplete(ArrayList<String> _array) {
    String _mydata = _array.get(0);
    System.out.println("hhhh" + _mydata);

}

@Override
public void onActionCompletes(HashMap<String, String> _map) {
    String _mydatas = _map.get("pankaj");
    System.out.println("thakur shab" + _mydatas);

}

【讨论】:

  • 先生,我必须在同一个类中调用 async.execute,当我尝试调用接口时,我收到错误说循环依赖
  • 根据您的代码,您只是为整个 android 应用程序制作了一个异步任务。那你能告诉我你为什么要使用界面吗?
  • 你可以在任何你想要的类中简单地调用异步任务,基本上当我们需要单个异步任务来完成应用程序时使用这个方法。
  • 上面的异步任务用在2个地方。在同一个Activity中,也在其他Activity中
  • 先生,我尝试使用单个构造函数调用,但它给了我错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-17
  • 2017-05-12
相关资源
最近更新 更多