我在列表视图中制作了一个json android应用程序,以显示照片的参考,但它给了我一个错误 我的代码是\
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发出请求 private static String url =“https://maps.googleapis.com/maps/api/place/nearbysearch / json?location = mylatandlong& radius = 5000& types = restaurant& sensor = false& key = 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 :(得分:0)
您忘记在将事件上下文初始化为c
上下文实例,然后将其传递给SimpleAdapter.so将活动上下文传递给适配器:
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
来更新UI元素形式onPostExecute