2013-05-15 30 views
1

我已經做了JSON的Android應用程序,以顯示照片的參考,在列表視圖中,但它給了我一個錯誤 我的代碼是\JSON例外解析photo_refrence來自谷歌Places API的

package com.example.jsonapp; 
import java.util.ArrayList; 
import java.util.HashMap; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.ListAdapter; 
import android.widget.SimpleAdapter; 

公共類MainActivity擴展ListActivity { // URL,使請求 私有靜態字符串URL = 「https://maps.googleapis.com/maps/api/place/nearbysearch/JSON位置= mylatandlong &半徑= 5000種&類型=餐廳&傳感器=假&鍵= myownkey?」;

// JSON Node names 
private static final String TAG_RESULTS = "results"; 
private static final String TAG_PHOTOS = "photos"; 
private static final String TAG_REFRENCE = "photo_reference"; 

JSONArray results = null; 
JSONArray photos = null; 

Context c; 

    // Progress dialog 
ProgressDialog pDialog; 
ArrayList<HashMap<String, String>> contactList; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 



    new LaodPhotos().execute(TAG_REFRENCE); 

    //ListView lv = getListView(); 

}//the end of on create 

public class LaodPhotos extends AsyncTask<String, String, String>{ 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Loading profile ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 
    @Override 
    protected String doInBackground(String... arg0) { 
     contactList = new ArrayList<HashMap<String, String>>(); 
     JSONParser jParser = new JSONParser(); 
     JSONObject json = jParser.getJSONFromUrl(url); 
     try{ 
      results = json.getJSONArray(TAG_RESULTS); 
      photos = json.getJSONArray(TAG_PHOTOS); 

      for(int i=0; i<results.length(); i++){ 
       JSONObject c = results.getJSONObject(i); 
       if(c != null){ 
        for(int j=0; j<photos.length(); j++){ 
         JSONObject pc = photos.getJSONObject(j); 
         String photo = pc.getString(TAG_REFRENCE); 
         // creating new HashMap 
         HashMap<String, String> map = new HashMap<String, String>(); 
         map.put(TAG_REFRENCE, photo); 
         contactList.add(map); 
        } 
       } 
      } 

     }catch(JSONException e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog after getting all products 
     pDialog.dismiss(); 
     // updating UI from Background Thread 
     runOnUiThread(new Runnable() { 
      public void run() { 

       ListAdapter adapter = new SimpleAdapter(c, contactList, R.layout.list_item, 
         new String[]{TAG_REFRENCE}, new int[]{R.id.photoRef}); 

       setListAdapter(adapter); 

      }//end of run method 
     });//end of runOnUiThread 
    }//end of onPost method 
    } 


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

}

日誌貓\ E/AndroidRuntime(23042):顯示java.lang.NullPointerException E/AndroidRuntime(23042):在android.widget.SimpleAdapter(SimpleAdapter.java:85 )

會有人幫助我知道如何使這個成功,我想有photo_reference 能夠顯示它爲圖像,但首先需要知道如何有photo_reference?!

+0

只是一個頭 - onPostExecute已經在UI線程上運行,沒有理由在另一個調用中包裝它。 –

回答

0

你忘記它傳遞給SimpleAdapter.so之前初始化與活動上下文語境c實例傳遞活動上下文到適配器:

ListAdapter adapter = new SimpleAdapter(MainActivity.this, 
             contactList, R.layout.list_item, 
             new String[]{TAG_REFRENCE}, 
             new int[]{R.id.photoRef}); 

MainActivity.this.setListAdapter(adapter); 

onPostExecute方法在UI線程已經運行,那麼沒有必要使用runOnUiThread爲更新用戶界面元素的形式onPostExecute

+0

很好,我已經做到了,但UI仍然沒有顯示任何內容,只有一個空白的白色屏幕。 –