【发布时间】:2015-08-20 11:00:06
【问题描述】:
我一直在尝试用编程方式创建的ImageViews 填充我的空HorizontalScrollView(因为我不知道它们会有多少;必须从网络上获取 JSON 数据才能找到)。此外,我必须使用 Fedor Vlasov 的 LazyList 设置 ImageViews 的显示图像(因为我从链接加载它)。我有以下代码:
private void displayListOfPlants() {
HorizontalScrollView scrollVPlants = (HorizontalScrollView) findViewById(R.id.scrollVPlants);
for (int i = 0; i < mPlantList.size(); i++) {
ImageView imgVPlant = new ImageView(CropRotationPlannerActivity.this);
scrollVPlants.addView(imgVPlant);
MainActivity.imageLoader.DisplayImage(mPlantList.get(i).get(InfoBookActivity.TAG_IMAGE_LOCATION), imgVPlant);
}
}
此方法在我的AsyncTask 的onPostExecute() 方法上调用。我在我的 Activity 的onResume() 方法中调用我的AsyncTask 的execute() 方法。
但是,当我运行代码时,出现以下错误:
08-20 04:43:38.253 2727-2727/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: thesleeplesselite.drgreenthumb, PID: 2727
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.fedorvlasov.lazylist.ImageLoader.DisplayImage(java.lang.String, android.widget.ImageView)' on a null object reference
at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity.displayListOfPlants(CropRotationPlannerActivity.java:146)
at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity.access$100(CropRotationPlannerActivity.java:28)
at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity$LoadPlants.onPostExecute(CropRotationPlannerActivity.java:166)
at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity$LoadPlants.onPostExecute(CropRotationPlannerActivity.java:150)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
在这一点上,我不知道该怎么办。我不知道为什么我的ImageViews 为空。
编辑:
我已将代码修改为以下内容,现在可以正常工作:
private void displayListOfPlants() {
LinearLayout linearLayoutForPlants = (LinearLayout) findViewById(R.id.linearLayoutForPlants);
LinearLayout.LayoutParams linearLayoutParamsForPlants = new LinearLayout.LayoutParams(20,20);
for (int i = 0; i < mPlantList.size(); i++) {
ImageView imgVPlant = new ImageView(CropRotationPlannerActivity.this);
imgVPlant.setLayoutParams(linearLayoutParamsForPlants);
linearLayoutForPlants.addView(imgVPlant);
MainActivity.imageLoader.DisplayImage(mPlantList.get(i).get(InfoBookActivity.TAG_IMAGE_LOCATION), imgVPlant);
}
}
所以,是的。对于像我这样的新手。请注意:
(1) 不能将ImageView 直接添加到ScrollView
(2) 确保添加LayoutParams
(3) 确保一切都已正确初始化。
注意:我将我的 JSON 标签作为静态字符串存储在 InfoBookActivity.java 中,因此我不必每次都创建类似的标签。
我在MainActivity.java 中创建了一个 ImageLoader 对象,以便在应用程序中整体使用。
【问题讨论】:
-
ImageView imgVPlant = new ImageView(CropRotationPlannerActivity.this);你确定这个和MainActivity.imageLoader.DisplayImage(mPlantList.get(i).get(InfoBookActivity.TAG_IMAGE_LOCATION), imgVPlant);这不为空吗? -
MainActivity.imageLoader.DisplayImage(mPlantList.get(i).get(InfoBookActivity.TAG_IMAGE_LOCATION), imgVPlant);这肯定是空的
-
我应该再检查一次。稍后回复。
-
imageLoader不为空。我在其他活动中使用过这个。这真的是ImageView,我仍然对如何不让它成为null感到困惑。 -
好的。我收回之前说过的话。你们俩都是对的。我的
imageLoader是null。奇怪的是,我像在其他两个活动中一样使用它,但从未出现过这个异常。很抱歉。
标签: java android android-asynctask imageview horizontalscrollview