我创建了一个自定义列表视图来解析JSON对象并在List中处理它!一切正常,但当我尝试通过单击ListView项目打开一个新的Intent时,应用程序崩溃了!我还使用内存缓存来存储从JSON对象url解析的图像,以便在ListView中显示它!
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
TextView rank;
TextView country;
TextView population;
ImageView flag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
rank = (TextView) itemView.findViewById(R.id.rank);
country = (TextView) itemView.findViewById(R.id.country);
population = (TextView) itemView.findViewById(R.id.population);
flag = (ImageView) itemView.findViewById(R.id.flag);
rank.setText(resultp.get(MainActivity.RANK));
country.setText(resultp.get(MainActivity.COUNTRY));
population.setText(resultp.get(MainActivity.POPULATION));
imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), flag);
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
intent.putExtra("rank", resultp.get(MainActivity.RANK));
intent.putExtra("country", resultp.get(MainActivity.COUNTRY));
intent.putExtra("population",resultp.get(MainActivity.POPULATION));
intent.putExtra("flag", resultp.get(MainActivity.FLAG));
context.startActivity(intent);
}
});
return itemView;
}
}
Logcat输出:
12-25 14:50:50.929: E/AndroidRuntime(1501): FATAL EXCEPTION: main
12-25 14:50:50.929: E/AndroidRuntime(1501): Process: com.android.jsonparse, PID: 1501
12-25 14:50:50.929: E/AndroidRuntime(1501): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.jsonparse/com.android.jsonparse.SingleItemView}: java.lang.NullPointerException
12-25 14:50:50.929: E/AndroidRuntime(1501): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
12-25 14:50:50.929: E/AndroidRuntime(1501): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-25 14:50:50.929: E/AndroidRuntime(1501): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-25 14:50:50.929: E/AndroidRuntime(1501): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-25 14:50:50.929: E/AndroidRuntime(1501): at android.os.Handler.dispatchMessage(Handler.java:102)
12-25 14:50:50.929: E/AndroidRuntime(1501): at android.os.Looper.loop(Looper.java:137)
12-25 14:50:50.929: E/AndroidRuntime(1501): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-25 14:50:50.929: E/AndroidRuntime(1501): at java.lang.reflect.Method.invokeNative(Native Method)
12-25 14:50:50.929: E/AndroidRuntime(1501): at java.lang.reflect.Method.invoke(Method.java:515)
12-25 14:50:50.929: E/AndroidRuntime(1501): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-25 14:50:50.929: E/AndroidRuntime(1501): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-25 14:50:50.929: E/AndroidRuntime(1501): at dalvik.system.NativeStart.main(Native Method)
12-25 14:50:50.929: E/AndroidRuntime(1501): Caused by: java.lang.NullPointerException
12-25 14:50:50.929: E/AndroidRuntime(1501): at android.content.ContextWrapper.getCacheDir(ContextWrapper.java:230)
12-25 14:50:50.929: E/AndroidRuntime(1501): at com.androidbegin.jsonparsetutorial.FileCache.<init>(FileCache.java:18)
12-25 14:50:50.929: E/AndroidRuntime(1501): at com.androidbegin.jsonparsetutorial.ImageLoader.<init>(ImageLoader.java:34)
12-25 14:50:50.929: E/AndroidRuntime(1501): at com.androidbegin.jsonparsetutorial.SingleItemView.<init>(SingleItemView.java:16)
12-25 14:50:50.929: E/AndroidRuntime(1501): at java.lang.Class.newInstanceImpl(Native Method)
12-25 14:50:50.929: E/AndroidRuntime(1501): at java.lang.Class.newInstance(Class.java:1208)
12-25 14:50:50.929: E/AndroidRuntime(1501): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-25 14:50:50.929: E/AndroidRuntime(1501): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)