Android AsyncTask doInBackground函数nullException错误

时间:2013-04-16 14:04:43

标签: android json android-asynctask argumentnullexception

大家好,我需要一些帮助我的代码。我想从Web服务器获取JSON数据,但是当我尝试调用doInBackground函数时,会出现nullException错误。

这是我的代码:

public class viewPOI extends ListActivity {
//private ProgressDialog progress;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> POIList;
private static String url_webservice = 
    "http://127.0.0.1/locaRemService/db_function.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_POIN = "point";
private static final String TAG_ALAMAT = "alamat_lokasi";
private static final String TAG_KONTEKS = "konteks_lokasi";
JSONArray POI = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_poi);

    POIList = new ArrayList<HashMap<String, String>>();
    new LoadAllPOI().execute();
    ListView Lpoi = getListView();
}

class LoadAllPOI extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jParser.getJSONFromURL(url_webservice, "GET", params);
        Log.d("All Products: ", json.toString());

        try {
            int sukses = json.getInt(TAG_SUCCESS);
            if(sukses == 1) {
                POI = json.getJSONArray(TAG_POIN);
                for(int i = 0; i < POI.length(); i++) {
                    JSONObject c = POI.getJSONObject(i);
                    String alamat = c.getString(TAG_ALAMAT);
                    String konteks = c.getString(TAG_KONTEKS);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_ALAMAT, alamat);
                    map.put(TAG_KONTEKS, konteks);
                    POIList.add(map);
                }
            } else {
                Toast.makeText(getApplicationContext(), "Tidak ada lokasi", Toast.LENGTH_SHORT).show();
            }
        } catch(JSONException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String file_url) {
        //progress.dismiss();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                ListAdapter adapter = new SimpleAdapter(viewPOI.this, POIList, 
                        R.layout.list_item, new String[]{TAG_ALAMAT, TAG_KONTEKS}, 
                        new int[]{R.id.alamatPOI, R.id.konteksPOI});
                setListAdapter(adapter);
            }

        });
    }
}

}

Log Cat

04-16 20:06:59.389: ERROR/AndroidRuntime(503): FATAL EXCEPTION: AsyncTask #1
04-16 20:06:59.389: ERROR/AndroidRuntime(503): java.lang.RuntimeException: An error occured while executing doInBackground()
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.lang.Thread.run(Thread.java:1019)
04-16 20:06:59.389: ERROR/AndroidRuntime(503): Caused by: java.lang.NullPointerException
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at com.locationReminder.viewPOI$LoadAllPOI.doInBackground(viewPOI.java:61)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at com.locationReminder.viewPOI$LoadAllPOI.doInBackground(viewPOI.java:1)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-16 20:06:59.389: ERROR/AndroidRuntime(503):     ... 4 more

感谢您的帮助......

[解决] 谢谢大家,我已经解决了这个问题。我将网址更改为10.0.2.2,问题结束了。谢谢大家...

2 个答案:

答案 0 :(得分:0)

首先,您为异步任务使用String,String,String泛型参数,因此您需要在execute上传递String参数(String param在这里)。 您还需要在doInBackground(String ... args)的末尾为onPostExecute(String file_url)返回一个String参数。在你的代码中,你只返回null。

答案 1 :(得分:0)

如果您正在使用this code,则可能因为多种原因(没有网络连接,网站错误,格式错误的JSON对象)而为您的对象获取空值。

而不是

Log.d("All Products: ", json.toString());

if (json!=null)
    Log.d("All Products: ", json.toString());

进一步审核:您的

try{...}  catch(JSONException ex) {...}

也会抛出异常,因为你没有在那里测试json的空值。所以

 if (json!=null){
    Log.d("All Products: ", json.toString());
     try{...}  catch(JSONException ex) {...}
 }

将是您的实际解决方案。