将图像JSON加载到列表视图

时间:2012-12-13 11:08:26

标签: android json web-services listview

我有一个列表视图,其中包含图像和图像名称,我正在从JSON Web服务加载数据并将其显示在列表视图中。

public class AndroidJSONParsingActivity extends ListActivity {

    // url to make request
    private static String url = "my url";

    // JSON Node names
     String Doctors = "Doctors";
     String DoctorName = "DoctorName";
     String ImageName = "ImageName";

     // contacts JSONArray
    JSONArray Doctor = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView image = (ImageView)findViewById(R.id.ImageView);
        // Hashmap for ListView
        ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of Contacts
            Doctor = json.getJSONArray(Doctors);

            // looping through All Contacts
            for(int i = 0; i < Doctor.length(); i++){
                JSONObject c = Doctor.getJSONObject(i);
                Log.d("TAG", "JSON IS: " + Doctor.toString());

                String imageBaseDirectory = "http://92.253.101.239:81/sudandoctors/UploadedFiles/";
                String imageName = c.getString(ImageName);//get image name from json parsing;
                System.err.println("imageBaseDirectory = "+imageBaseDirectory);
                System.err.println("imageName = "+imageName);
                System.err.println("two = "+imageBaseDirectory+imageName);
                image.setImageURI(Uri.parse(imageBaseDirectory+imageName));

                String n = imageBaseDirectory+imageName;
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put("DoctorName", c.getString("DoctorName"));
                map.put("ImageName", n);

                // adding HashList to ArrayList
                contactList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(this, contactList,
                R.layout.list_item,
                new String[] { DoctorName, ImageName }, new int[] {
                        R.id.name, R.id.ImageView });

        setListAdapter(adapter);


    }

}

我收到了这个错误

12-13 14:06:07.793: E/AndroidRuntime(1148): FATAL EXCEPTION: main
12-13 14:06:07.793: E/AndroidRuntime(1148): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.jsonparsing/com.androidhive.jsonparsing.AndroidJSONParsingActivity}: java.lang.NullPointerException
12-13 14:06:07.793: E/AndroidRuntime(1148):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at android.os.Looper.loop(Looper.java:123)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at java.lang.reflect.Method.invoke(Method.java:507)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at dalvik.system.NativeStart.main(Native Method)
12-13 14:06:07.793: E/AndroidRuntime(1148): Caused by: java.lang.NullPointerException
12-13 14:06:07.793: E/AndroidRuntime(1148):     at com.androidhive.jsonparsing.AndroidJSONParsingActivity.onCreate(AndroidJSONParsingActivity.java:61)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-13 14:06:07.793: E/AndroidRuntime(1148):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-13 14:06:07.793: E/AndroidRuntime(1148):     ... 11 more

当我在浏览器中使用链接时,它实际上给了我图片,那么问题是什么