1

我制作了一个 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;

public class MainActivity extends ListActivity { // url 请求私有 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;
}

}

日志 cat\ E/AndroidRuntime(23042): java.lang.NullPointerException E/AndroidRuntime(23042): at android.widget.SimpleAdapter.(SimpleAdapter.java:85)

有人能帮我知道如何让这件事成功吗,我想让 photo_reference 能够将其显示为图像,但首先需要知道如何拥有 photo_reference?!

4

1 回答 1

0

c在将Context 实例传递给 SimpleAdapter 之前,您忘记使用 Activity Context 对其进行初始化。因此,将 Activity Context 传递给 Adapter,如下所示:

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

于 2013-05-15T02:58:44.863 回答