在AsyncTask中调用IF条件

时间:2014-12-10 04:38:46

标签: android multithreading android-asynctask

我正在调用AsyncTask中的条件。但是当运行应用程序时它只执行if内的第一个else条件应用程序崩溃。 我在这做错了什么?

class LoadingModuleLayout extends AsyncTask<String, String, String> {
@Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.d("Loading", "Menu Modules Loading");
        }

        @Override
        protected String doInBackground(String... args) {

            Log.d("module_name", module_name);
            String response = null;
            Map<String, Object> data = new LinkedHashMap<String, Object>();
            data.put(SESSION, sessionId);
            data.put(MODULE_NAME, module_name);
            data.put(QUERY, query != null ? query.toLowerCase() : "");
            data.put(ORDER_BY, orderBy != null ? orderBy : "");
            data.put(OFFSET, offset != null ? offset : "");
            data.put(
                    SELECT_FIELDS,
                    (selectFields != null && selectFields.length != 0) ? new JSONArray(
                            Arrays.asList(selectFields)) : "");

            try {
                JSONArray nameValueArray = new JSONArray();
                if (linkNameToFieldsArray != null
                        && linkNameToFieldsArray.size() != 0) {
                    for (Entry<String, List<String>> entry : linkNameToFieldsArray
                            .entrySet()) {
                        JSONObject nameValue = new JSONObject();
                        nameValue.put("name", entry.getKey());
                        nameValue.put("value", new JSONArray(entry.getValue()));
                        nameValueArray.put(nameValue);
                    }
                }
                data.put(LINK_NAME_TO_FIELDS_ARRAY, nameValueArray);
                data.put(MAX_RESULTS, maxResults != null ? maxResults : "");
                data.put(DELETED, deleted != null ? deleted : 0);

                String restData = org.json.simple.JSONValue.toJSONString(data);

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost req = new HttpPost(restURL);
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair(METHOD,
                        GET_ENTRY_LIST));
                nameValuePairs.add(new BasicNameValuePair(INPUT_TYPE, JSON));
                nameValuePairs.add(new BasicNameValuePair(RESPONSE_TYPE, JSON));
                nameValuePairs.add(new BasicNameValuePair(REST_DATA, restData));
                req.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Send POST request
                httpClient.getParams().setBooleanParameter(
                        CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
                HttpResponse res = httpClient.execute(req);
                response = EntityUtils.toString(res.getEntity());
                if (response == null) {
                    Log.e("LOG_TAG", "FAILED TO CONNECT!");

                }
                // Log.e("LOG_TAG", "response is : " + response);
                JSONObject responseJObj = new JSONObject(response);
                // Log.d("responseJObj", responseJObj.toString());
                JSONArray jArray = responseJObj.getJSONArray(ENTRY_LIST);

                for (int i = 0; i < jArray.length(); i++) {

                    JSONObject obj = jArray.getJSONObject(i);
                    //Log.d("obj", obj.toString());
                    //JSONObject objName = new JSONObject("name");
                    id = obj.getString("id");

                    JSONObject name_value_list = obj
                            .getJSONObject("name_value_list");

                    if(module_name.equals("Accounts")){
                        JSONObject name = name_value_list.getJSONObject("name");
                        acc_name = name.getString("value");

                        entry_names.add(acc_name);
                    }
                    else if(module_name.equals("Contacts")){
                        JSONObject first_name = name_value_list.getJSONObject("first_name");
                        cnt_f_name = first_name.getString("value");
                        JSONObject last_name = name_value_list.getJSONObject("last_name");
                        cnt_l_name = last_name.getString("value");
                        cnt_name = cnt_f_name+" "+cnt_l_name;

                        entry_names.add(cnt_name);
                    }



                    entry_ids.add(id);


                }

                Log.d("value ", acc_name);


            } catch (JSONException jo) {
                // Log.e("LOG_TAG", "response is : " + response);

            } catch (IOException ioe) {

            }
            return null;

        }

        protected void onPostExecute(String file_url) {


                Log.d(TAG,entry_names.toString());
                EntriesListAdapter draweradapter= new EntriesListAdapter(getActivity()
                        .getApplicationContext(), entry_names);
                mEntryList.setAdapter(draweradapter);

        }
    } 

----- ---- logcat中

12-10 09:52:36.637: E/AndroidRuntime(1621): FATAL EXCEPTION: AsyncTask #2
12-10 09:52:36.637: E/AndroidRuntime(1621): Process: com.wakensys.sugercrm_wakensys, PID: 1621
12-10 09:52:36.637: E/AndroidRuntime(1621): java.lang.RuntimeException: An error occured while executing doInBackground()
12-10 09:52:36.637: E/AndroidRuntime(1621):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at java.lang.Thread.run(Thread.java:841)
12-10 09:52:36.637: E/AndroidRuntime(1621): Caused by: java.lang.NullPointerException: println needs a message
12-10 09:52:36.637: E/AndroidRuntime(1621):     at android.util.Log.println_native(Native Method)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at android.util.Log.d(Log.java:139)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at com.wakensys.sugercrm_wakensys.fragments.Fragment_Entries$LoadingModuleLayout.doInBackground(Fragment_Entries.java:240)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at com.wakensys.sugercrm_wakensys.fragments.Fragment_Entries$LoadingModuleLayout.doInBackground(Fragment_Entries.java:1)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-10 09:52:36.637: E/AndroidRuntime(1621):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)

2 个答案:

答案 0 :(得分:2)

始终尝试记录,就像这样

Log.d("module_name", ""+module_name);

而不是

Log.d("module_name", module_name);

通过使用此方法,您可以避免使用NullPointerExceptions

答案 1 :(得分:1)

您正在将null传递给日志语句。我想,看看240行。