Android的AsyncTask的doInBackground功能nullException错误 [英] Android AsyncTask doInBackground function nullException error

查看:152
本文介绍了Android的AsyncTask的doInBackground功能nullException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi everybody i need some help with my code. I want to get JSON data from web server but when i try to call the doInBackground function nullException error appear.

这是我的code:

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

        });
    }
}

}

日志猫

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

感谢您的帮助...

Thanks for any help...

[解决]
感谢大家对我有这个问题做。我将URL更改为10.0.2.2,问题就结束了。感谢所有...

[SOLVED] Thanks for everybody i have done with this problem. I change the url to 10.0.2.2 and the problem is over. Thanks for all...

推荐答案

如果你使用的这code ,你可能获得你的,因为很多原因(没有净连接,错误的网站,畸形的JSON对象)一个对象的空值。

If you're using this code, you're probably getting a null value for your object because of one of many reasons (no net connection, wrong site, malformed JSON Object).

而不是

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) {...}
 }

将是你的实际的解决方案。

would be your actual solution.

这篇关于Android的AsyncTask的doInBackground功能nullException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆