2015-02-24 21 views
0

所以我有一个问题。我一直试图让http请求工作一段时间,而且我创建了一个新线程,以便请求不会在主线程上运行。我已经成功地做到了这一点,以便发生HTTP请求,并且在请求线程中,我已经完成了它,它记录了它从http请求中获取的数据。现在如果我想在方法外部使用这个jsonObject,它会给我带来错误。帮帮我?HTTP请求Android - 试图调用虚拟方法

的logcat:

02-23 22:48:08.612 4239-4239/com.tamrefrank.www.listviewandroidstudio E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.tamrefrank.www.listviewandroidstudio, PID: 4239 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tamrefrank.www.listviewandroidstudio/com.tamrefrank.www.listviewandroidstudio.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONArray.toString()' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONArray.toString()' on a null object reference 
      at com.tamrefrank.www.listviewandroidstudio.MainActivity.onCreate(MainActivity.java:38) 
      at android.app.Activity.performCreate(Activity.java:5933) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
02-23 22:48:09.332 4239-4254/com.tamrefrank.www.listviewandroidstudio I/log_tag﹕ [{"Name":"Cabinet","Location":"In Storage","Latitude":"40.83990100","Longitude":"14.25185000","pk":19587,"image_id":"2006AJ6728"},{"Name":"Panel","Location":"Medieval and Renaissance, room 8, case WS EXP","Latitude":"48.87287900","Longitude":"4.63710000","pk":11371,"image_id":"2006AP3163"},{"Name":"Wallpaper","Location":"British Galleries, room 122f, case 1","Latitude":"53.47962200","Longitude":"-2.24881000","pk":19492,"image_id":"2006AU6265"},{"Name":"Portrait miniature","Location":"British Galleries, room 58c, case 2","Latitude":"52.88328900","Longitude":"-1.97685000","pk":9491,"image_id":"2006AH0271"},{"Name":"Caricature","Location":"In Storage","Latitude":"53.02513900","Longitude":"-2.18676000","pk":29979,"image_id":"2006AE9142"},{"Name":"Card game","Location":"In Storage","Latitude":"52.88328900","Longitude":"-1.97685000","pk":26483,"image_id":"2006AU5916"},{"Name":"Tile","Location":"Islamic Middle East, room 42, case WW9","Latitude":"39.76586200","Longitude":"64.42234000","pk":30220,"image_id":"2006AY5617"},{"Name":"Sculpture","Location":"Sculpture, room 22, case 2","Latitude":"52.88328900","Longitude":"-1.97685000","pk":36026,"image_id":"2006AM7780"},{"Name":"Oil painting","Location":"Prints & Drawings Study Room, room 315, case 24, shelf R","Latitude":"52.88328900","Longitude":"-1.97685000","pk":6905,"image_id":"2006AU6480"},{"Name":"Snuff bottle","Location":"In Storage","Latitude":"36.89445100","Longitude":"104.16564900","pk":10895,"image_id":"2006AU8783"},{"Name":"Snuff bottle","Location":"In Storage","Latitude":"36.89445100","Longitude":"104.16564900","pk":10902,"image_id":"2006AU9234"}] 
02-23 22:48:09.332 4239-4254/com.tamrefrank.www.listviewandroidstudio I/log_tag﹕ [{"Name":"Cabinet","Location":"In Storage","Latitude":"40.83990100","Longitude":"14.25185000","pk":19587,"image_id":"2006AJ6728"},{"Name":"Panel","Location":"Medieval and Renaissance, room 8, case WS EXP","Latitude":"48.87287900","Longitude":"4.63710000","pk":11371,"image_id":"2006AP3163"},{"Name":"Wallpaper","Location":"British Galleries, room 122f, case 1","Latitude":"53.47962200","Longitude":"-2.24881000","pk":19492,"image_id":"2006AU6265"},{"Name":"Portrait miniature","Location":"British Galleries, room 58c, case 2","Latitude":"52.88328900","Longitude":"-1.97685000","pk":9491,"image_id":"2006AH0271"},{"Name":"Caricature","Location":"In Storage","Latitude":"53.02513900","Longitude":"-2.18676000","pk":29979,"image_id":"2006AE9142"},{"Name":"Card game","Location":"In Storage","Latitude":"52.88328900","Longitude":"-1.97685000","pk":26483,"image_id":"2006AU5916"},{"Name":"Tile","Location":"Islamic Middle East, room 42, case WW9","Latitude":"39.76586200","Longitude":"64.42234000","pk":30220,"image_id":"2006AY5617"},{"Name":"Sculpture","Location":"Sculpture, room 22, case 2","Latitude":"52.88328900","Longitude":"-1.97685000","pk":36026,"image_id":"2006AM7780"},{"Name":"Oil painting","Location":"Prints & Drawings Study Room, room 315, case 24, shelf R","Latitude":"52.88328900","Longitude":"-1.97685000","pk":6905,"image_id":"2006AU6480"},{"Name":"Snuff bottle","Location":"In Storage","Latitude":"36.89445100","Longitude":"104.16564900","pk":10895,"image_id":"2006AU8783"},{"Name":"Snuff bottle","Location":"In Storage","Latitude":"36.89445100","Longitude":"104.16564900","pk":10902,"image_id":"2006AU9234"}] 
02-23 22:48:32.712 4239-4239/com.tamrefrank.www.listviewandroidstudio I/Process﹕ Sending signal. PID: 4239 SIG: 9 

代码:

package com.tamrefrank.www.listviewandroidstudio; 
//All the imports happen when you auto complete. Often you may have erros when you copy paste things directly because libraries may not get imported 
import android.os.AsyncTask; 
import android.os.StrictMode; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.IOException; 
import java.io.InputStream; 


public class MainActivity extends ActionBarActivity { 
    private String [] drinks; 
    JSONArray jsonObject; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //set the content view to activity_main.xml 
     setContentView(R.layout.activity_main); 

     JSONArray object = new JSONArray(); 
     object = getData(); 
     Log.i("log_tag", jsonObject.toString()); 

//  System.out.print("success: " + object.toString()); 


     drinks = new String[3]; 
     drinks[0] = "hi"; 
     drinks[1] = "5"; 
     drinks[2] = "3"; 
/** 
    * Lists require adapters-Adapters are the place that we store the various data items we want 
    to appear in the Lists. This will be handled in an adapter. 
    */ 

     ArrayAdapter<String> adapter; 

//We will create the array adapter and reference it to our individual List item from the ListView and then to the array we just created- Drinks. 
     adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,drinks); 

//We finally will connect the adapter to the List View 
     ListView drinkLists = (ListView) findViewById(R.id.listView); 

     //Set the drinks array to the adapter below 
     drinkLists.setAdapter(adapter); 
    } 

    public JSONArray getData(){ 
     new Thread(new Runnable() { 
       public void run() { 

        HttpGet httpGet = new HttpGet("https://stampwatch.herokuapp.com/"); 
        HttpClient client = new DefaultHttpClient(); 
        HttpResponse response; 
        StringBuilder stringBuilder = new StringBuilder(); 

        try { 
         response = client.execute(httpGet); 
         HttpEntity entity = response.getEntity(); 
         InputStream stream = entity.getContent(); 
         int b; 
         while ((b = stream.read()) != -1) { 
          stringBuilder.append((char) b); 
         } 
        } catch (ClientProtocolException e) { 
        } catch (IOException e) { 
         System.out.println(e); 
        } 

        try { 
         jsonObject = new JSONArray(stringBuilder.toString()); 
         Log.i("log_tag", jsonObject.toString()); 
; 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        Log.i("log_tag", jsonObject.toString()); 
        } 

      }).start(); 
      return jsonObject; 
     } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 
+0

@RenéLink写一个答案,我会+1并接受它。谢谢 – LucaSpeedStack 2015-02-24 07:01:18

回答

2

这里:

Log.i("log_tag", jsonObject.toString()); 

NPE因为jsonObjectnull并试图使用空对象调用toString()方法的JSONObject。

但是,为什么如果我用该对象发出请求,对象为空?

因为jsonObject对象在getData();内被初始化,其中一个线程用于从api获取数据。意味着:直到刚才线程完成启动线程,而不是等待后

Log.i("log_tag", jsonObject.toString()); 

行执行。

+0

但是,如果我用这个对象发出了一个请求,为什么这个对象是空的? – LucaSpeedStack 2015-02-24 07:00:44

+0

@LucaSpeedStack:看到我的编辑答案可能明白为什么'jsonObject'为空 – 2015-02-24 07:07:05

+0

@LucaSpeedStack:如果在从服务器获得响应后使用'AsyncTask'而不是Thread来更新UI数据 – 2015-02-24 07:07:43

相关问题