2012-12-13 188 views
-2

我有一個列表視圖,其中有一個圖像和圖像名稱,我從JSON Web服務加載數據並將其顯示在列表視圖中。加載圖像JSON列表視圖

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 

,當我使用我的瀏覽器中的鏈接,它實際上給我PIC,所以有什麼問題

+0

他們在行數61是什麼? –

+0

image.setImageURI(Uri.parse(imageBaseDirectory + imageName)); –

+0

你爲什麼要設置imageUri 2次? –

回答