我按照this使用json创建列表视图。在列表视图中选择单个Iten时出现以下错误
代码是..
公共类SingleViewNews扩展Activity {
字符串标题;
字符串日期;
String desc;
字符串图像;
字符串位置;
ImageLoader imageLoader = new ImageLoader(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oneview_news);
Intent i = getIntent();
title = i.getStringExtra("Title");
date = i.getStringExtra("Date");
desc = i.getStringExtra("Description");
image = i.getStringExtra("Image");
TextView txtrank = (TextView) findViewById(R.id.title);
TextView txtcountry = (TextView) findViewById(R.id.date);
TextView txtpopulation = (TextView) findViewById(R.id.desc);
ImageView imgflag = (ImageView) findViewById(R.id.n_img);
txtrank.setText(title);
txtcountry.setText(date);
txtpopulation.setText(desc);
imageLoader.DisplayImage(image, imgflag);
}
}
08-05 11:50:42.957: E/AndroidRuntime(367): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bulidersapp/com.example.bulidersapp.SingleViewNews}: java.lang.NullPointerException
08-05 11:50:42.957: E/AndroidRuntime(367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
08-05 11:50:42.957: E/AndroidRuntime(367): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-05 11:50:42.957: E/AndroidRuntime(367): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-05 11:50:42.957: E/AndroidRuntime(367): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-05 11:50:42.957: E/AndroidRuntime(367): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 11:50:42.957: E/AndroidRuntime(367): at android.os.Looper.loop(Looper.java:123)
08-05 11:50:42.957: E/AndroidRuntime(367): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-05 11:50:42.957: E/AndroidRuntime(367): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 11:50:42.957: E/AndroidRuntime(367): at java.lang.reflect.Method.invoke(Method.java:507)
08-05 11:50:42.957: E/AndroidRuntime(367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-05 11:50:42.957: E/AndroidRuntime(367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-05 11:50:42.957: E/AndroidRuntime(367): at dalvik.system.NativeStart.main(Native Method)
08-05 11:50:42.957: E/AndroidRuntime(367): Caused by: java.lang.NullPointerException
08-05 11:50:42.957: E/AndroidRuntime(367): at android.content.ContextWrapper.getCacheDir(ContextWrapper.java:188)
08-05 11:50:42.957: E/AndroidRuntime(367): at com.example..FileCache.<init>(FileCache.java:19)
08-05 11:50:42.957: E/AndroidRuntime(367): at com.example..ImageLoader.<init>(ImageLoader.java:34)
08-05 11:50:42.957: E/AndroidRuntime(367): at com.example..SingleViewNews.<init>(SingleViewNews.java:17)
08-05 11:50:42.957: E/AndroidRuntime(367): at java.lang.Class.newInstanceImpl(Native Method)
08-05 11:50:42.957: E/AndroidRuntime(367): at java.lang.Class.newInstance(Class.java:1409)
08-05 11:50:42.957: E/AndroidRuntime(367): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-05 11:50:42.957: E/AndroidRuntime(367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
08-05 11:50:42.957: E/AndroidRuntime(367): ... 11 more
请帮我解决此问题
答案 0 :(得分:1)
您在SingleViewNews
阶段过早地将Context
活动用作<init>
,即构造函数或成员变量初始化。
您只能在[{1}}或更高版本中将活动用作Context
。
将onCreate()
初始化移至ImageLoader
,例如变化
onCreate()
到
ImageLoader imageLoader = new ImageLoader(this);
然后在ImageLoader imageLoader;
:
onCreate()