方法在空引用上调用 - JSON

时间:2015-12-02 16:38:07

标签: php android json database

我想创建将使用外部数据库的应用程序。从php文件中读取数据时遇到问题。 PHP代码正在运行,因为我在浏览器中测试它并显示我希望它显示的代码。我在后台阅读数据时遇到问题。

Logcat看起来像这样:

java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:309)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.net.URI.isAbsolute()' on a null object reference
    at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:501)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
    at com.mycompany.photoapplication.JSONParser.makeHttpRequest(JSONParser.java:53)
    at com.mycompany.photoapplication.MainActivity$LoadAllPhotos.doInBackground(MainActivity.java:82)
    at com.mycompany.photoapplication.MainActivity$LoadAllPhotos.doInBackground(MainActivity.java:68)
    at android.os.AsyncTask$2.call(AsyncTask.java:295)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
    at java.lang.Thread.run(Thread.java:818) 

我的inBackground代码如下所示:

protected String doInBackground(String... args) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();

    JSONObject json = jParser.makeHttpRequest(url_all_photos, "GET", params);

    try {
        int success = json.getInt(TAG_SUCCESS);

        if (success == 1) {
            photos = json.getJSONArray(TAG_PRODUCTS);
            for (int i = 0; i < photos.length(); i++) {
                JSONObject c = photos.getJSONObject(i);

                id[i] = c.getString(TAG_PID);
                photoTitle[i] = c.getString(TAG_TITLE);
                photoDescription[i] = c.getString(TAG_DESCRIPTION);
                photoStamp[i] = c.getString(TAG_TIMESTAMP);
                photo = c.getString(TAG_PHOTO);
                bitmap[i] = decodeBase4(photo);
            }
        } else {
            Intent i = new Intent(getApplicationContext(),
                    photoActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);                        startActivity(i);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}

1 个答案:

答案 0 :(得分:0)

我认为问题在于您在主线程之外使用意图。

你应该使用onPostExecute ......就像这样:

@Override
protected void onPostExecute(Boolean result) {
    Intent i = new Intent(getApplicationContext(),photoActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
    startActivity(i);
}