image calling using Json format
I can retrieve normal list from the server but when i am trying to set up the image, its producing "Force to close" . The json file can be viewwd here .A bundle of Thanks in advance. Here is the code.
public class Test extends ListActivity {
Prefs myprefs = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
this.myprefs = new Prefs(getApplicationContext());
// install handler for processing gui update messages
ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/proj/androidjson.php?identifier=" +
Test.this.myprefs.getPersonalno());
try{
JSONArray earthquakes = json.getJSONArray("services");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, Object> map = new HashMap<String, Object>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", e.getString("taskid"));
map.put("pic", "Service name : " + e.getString("employeepic"));
map.put("serviceinfo", "" + e.getString("employeename")+ " : "+ e.getString("starttime")
+" To " + e.getStrin开发者_JAVA百科g("endtime"));
ImageView imgView=(ImageView)findViewById(R.id.image);
String imageBaseDirectory = "http://midsweden.gofreeserve.com/proj/admin/pictures";
String imageName = e.getString("employeepic");
imgView.setImageURI(Uri.parse(imageBaseDirectory+imageName));
map.put("img",imageName);
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
SimpleAdapter adapter = new SimpleAdapter(this, mylist , R.layout.test,new String[] {"img", "servicename", "serviceinfo" },
new int[] { R.id.image ,R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Test.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
Here is the xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#55000000"
android:background="#55000000"
>
<ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="50dip"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="20dp" />
<TextView
android:id="@+id/item_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="18dp" />
</LinearLayout>
Here is the logcat
07-06 01:09:18.409: WARN/dalvikvm(101): disableGcForExternalAlloc: true
07-06 01:09:18.409: INFO/ActivityManager(101): Starting activity: Intent { cmp=com.droidnova.android.howto.optionmenu/.Test }
07-06 01:09:18.449: WARN/dalvikvm(101): disableGcForExternalAlloc: false
07-06 01:09:19.009: DEBUG/dalvikvm(16327): GC freed 2412 objects / 190240 bytes in 105ms
07-06 01:09:20.829: DEBUG/ImagesWidgetProvider(16291): OnReceive:Action: android.appwidget.action.APPWIDGET_UPDATE
07-06 01:09:20.829: DEBUG/ImagesWidgetProvider(16291): onUpdate():
07-06 01:09:21.329: DEBUG/AndroidRuntime(16327): Shutting down VM
07-06 01:09:21.329: WARN/dalvikvm(16327): threadid=3: thread exiting with uncaught exception (group=0x4001e390)
07-06 01:09:21.329: ERROR/AndroidRuntime(16327): Uncaught handler: thread main exiting due to uncaught exception
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at android.app.ActivityThread.access$2200(ActivityThread.java:126)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at android.os.Looper.loop(Looper.java:123)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at android.app.ActivityThread.main(ActivityThread.java:4595)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at java.lang.reflect.Method.invoke(Method.java:521)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at dalvik.system.NativeStart.main(Native Method)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): Caused by: java.lang.NullPointerException
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:56)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-06 01:09:21.339: ERROR/AndroidRuntime(16327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
Two possibilities:
- e has no element keyed 'employeepic'
- there is no R.id.image
It's hard to tell, the way the code is laid out.
精彩评论