【问题标题】:NullPointerException on ImageViewImageView 上的 NullPointerException
【发布时间】: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);
    }
}

此方法在我的AsyncTaskonPostExecute() 方法上调用。我在我的 Activity 的onResume() 方法中调用我的AsyncTaskexecute() 方法。

但是,当我运行代码时,出现以下错误:

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感到困惑。
  • 好的。我收回之前说过的话。你们俩都是对的。我的imageLoadernull。奇怪的是,我像在其他两个活动中一样使用它,但从未出现过这个异常。很抱歉。

标签: java android android-asynctask imageview horizontalscrollview


【解决方案1】:

最近,当我尝试调用 imageLoader.DisplayImage() 来查看 ImageView 中的图像时,我在代码中遇到了相同的错误(imageLoader 的空对象引用),我得到了同样的错误。现在我已经找到了这个错误的解决方案。

请用当前activity初始化imageLoader,使用如下代码:

imageLoader = new ImageLoader(this);

这里的“this”是当前活动的对象, 现在你在这段代码之后调用 DisplayImage()

希望这段代码对你有帮助

【讨论】:

    【解决方案2】:

    您不能直接将Imagviews 添加到Scrollview,您必须向其添加线性布局,然后将imageviews 添加到linearlayout

    【讨论】:

    • 感谢您指出这一点。我已经相应地修改了我的代码。但是,我仍然收到 NullPointerException。我开始认为问题在于ImageView 的声明和初始化。
    【解决方案3】:

    实际上你创建了ImageView,但是你没有将params添加到imageView。而且你直接将imageView添加到ScrollView。将Linear Layout添加到ScrollView,然后将ImageView添加到LinearLayout。我认为这就是为什么您会收到 空指针异常。首先将参数添加到您的 imageView 并重试。

    【讨论】:

    • 我更新了我的代码。尽管如此,我还是得到了 NullPointerException。
    • 你确定这个LinearLayout id R.id.linearLayoutForPlants 存在于你的布局中,我的意思是在Horizo​​ntalscrollview 中
    • 是的。我将此活动添加到我的 xml 文件中。
    • 在 oncreate LinearLayout linearLayoutForPlants = (LinearLayout) findViewById(R.id.linearLayoutForPlants) 中声明这一行;
    • 非常感谢您提供帮助并指出 LayoutParams 的事情。我发现了问题。我的imageLoader 是罪魁祸首。我从来没有这样想过,因为我在其他课程中使用过两次,并且没有出现任何错误。
    猜你喜欢
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 2014-03-18
    • 2019-10-24
    • 2016-03-26
    相关资源
    最近更新 更多