gpt4 book ai didi

android - ImageSwitcher 中的 setImageDrawable 和 setImageResource 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 18:21:36 25 4
gpt4 key购买 nike

当我尝试使用资源填充 ImageSwitcher 时,我不断收到 NullPointerException 错误。它在 onPreExecute() 期间使用 AsyncTask 中的 WeakReference 调用:

if (imageSwitcherReference != null) {
ImageSwitcher imageSwitcher = imageSwitcherReference.get();
if (imageSwitcher != null) {
imageSwitcher.setImageResource(R.drawable.receta_nofoto);
}
}

这是预堆栈跟踪:

02-21 16:13:10.639: ERROR/AndroidRuntime(4274): java.lang.NullPointerException
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at android.widget.ImageSwitcher.setImageResource(ImageSwitcher.java:41)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at com.paravegetarianos.motores.DescargadorImagenes.onPreExecute(DescargadorImagenes.java:90)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at android.os.AsyncTask.execute(AsyncTask.java:391)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at com.paravegetarianos.FotosGaleria.seleccionarImagen(FotosGaleria.java:57)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at com.paravegetarianos.FotosGaleria.access$0(FotosGaleria.java:53)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at com.paravegetarianos.FotosGaleria$1.onItemSelected(FotosGaleria.java:43)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at android.widget.AdapterView.access$200(AdapterView.java:42)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at android.os.Handler.handleCallback(Handler.java:587)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at android.os.Looper.loop(Looper.java:123)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at java.lang.reflect.Method.invoke(Method.java:521)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-21 16:13:10.639: ERROR/AndroidRuntime(4274): at dalvik.system.NativeStart.main(Native Method)

当尝试使用我刚刚创建的 BitmapDrawable 设置 Drawable 时,它​​也会变成 NullPointerException。

if (imageSwitcherReference != null) {
ImageSwitcher imageSwitcher = imageSwitcherReference.get();
if (imageSwitcher != null) {
BitmapDrawable bm = new BitmapDrawable(result);
imageSwitcher.setImageDrawable(bm);
}
}

result 是在 onPostExecute() 步骤中捕获的位图。我调用 AsyncTask 的那一刻是在 onItemSelectedListener() 时,在构建画廊时:

    galeria.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
seleccionarImagen(arg2);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
seleccionarImagen(0);
}
});

其中 galeria 是 Gallery,seleccionarImagen(int) 获取图像索引并调用 AsyncTask。

这是帖子堆栈跟踪(在注释错误行之后):

02-21 16:04:42.849: ERROR/AndroidRuntime(4201): FATAL EXCEPTION: main
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): java.lang.NullPointerException
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at android.widget.ImageSwitcher.setImageDrawable(ImageSwitcher.java:55)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at com.paravegetarianos.motores.DescargadorImagenes.onPostExecute(DescargadorImagenes.java:66)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at com.paravegetarianos.motores.DescargadorImagenes.onPostExecute(DescargadorImagenes.java:1)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at android.os.AsyncTask.finish(AsyncTask.java:417)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at android.os.AsyncTask.access$300(AsyncTask.java:127)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at android.os.Looper.loop(Looper.java:123)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at java.lang.reflect.Method.invoke(Method.java:521)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-21 16:04:42.849: ERROR/AndroidRuntime(4201): at dalvik.system.NativeStart.main(Native Method)

你能找到这段代码的任何问题(我敢打赌这将是一个非常愚蠢和明显的错误)吗?感谢大家。

PS:错误行(66 和 90)是调用 setImageDrawable 和 setImageResource 的地方。如果您认为需要更多代码,请告诉我,我会附加它。

最佳答案

您需要将 View 添加到您的 ImageSwitcher,方法是设置 ViewFactory 或调用 addView 两次。更多信息在 docs for ViewSwitcher .

例如,查看问题中的最后一个堆栈跟踪,NPE 发生在 ImageSwitcher.java 的第 55 行,即:

public void setImageDrawable(Drawable drawable)
{
ImageView image = (ImageView)this.getNextView();
image.setImageDrawable(drawable); // <--- line 55
showNext();
}

image 为空,因为尚未设置 View 。

关于android - ImageSwitcher 中的 setImageDrawable 和 setImageResource 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5067677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com