gpt4 book ai didi

android - IMageSwitcher.setImageResource() 中的空点异常

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:26 26 4
gpt4 key购买 nike

我正在制作一个动画以在 ImageSwitcher 中显示许多图像,但我无法将图像设置为 ImageSwitcher

这是我的 xml 中的图像切换器。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:src="@drawable/logo" />

<ImageSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ImageSwitcher>

</LinearLayout>

这是我的代码

setContentView(R.layout.main_screen);

final int[] imgs = {R.drawable.m1,
R.drawable.m2,
R.drawable.m3,
R.drawable.m4,
R.drawable.m5,
R.drawable.m6};

imgswitcher = (ImageSwitcher) findViewById(R.id.switcher);
imgswitcher.postDelayed(new Runnable()
{
int index = 0;
@Override
public void run()
{
imgswitcher.setImageResource(imgs[index]);
if(index==(imgs.length-1))
index = 0;
else
index++;
imgswitcher.postDelayed(this, 1000);
}
}, 1000);

我在行“imgswitcher.setImageResource(imgs[index]);”上遇到错误

错误

11-13 15:08:34.574: E/AndroidRuntime(5258): FATAL EXCEPTION: main
11-13 15:08:34.574: E/AndroidRuntime(5258): java.lang.NullPointerException
11-13 15:08:34.574: E/AndroidRuntime(5258): at android.widget.ImageSwitcher.setImageResource(ImageSwitcher.java:41)
11-13 15:08:34.574: E/AndroidRuntime(5258): at com.example.imageswitcher.MainActivity$1.run(MainActivity.java:33)
11-13 15:08:34.574: E/AndroidRuntime(5258): at android.os.Handler.handleCallback(Handler.java:587)
11-13 15:08:34.574: E/AndroidRuntime(5258): at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 15:08:34.574: E/AndroidRuntime(5258): at android.os.Looper.loop(Looper.java:123)
11-13 15:08:34.574: E/AndroidRuntime(5258): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-13 15:08:34.574: E/AndroidRuntime(5258): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 15:08:34.574: E/AndroidRuntime(5258): at java.lang.reflect.Method.invoke(Method.java:507)
11-13 15:08:34.574: E/AndroidRuntime(5258): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-13 15:08:34.574: E/AndroidRuntime(5258): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-13 15:08:34.574: E/AndroidRuntime(5258): at dalvik.system.NativeStart.main(Native Method)

我什至在没有 Runnable 的情况下尝试过,只是将一个 drawable 分配给 ImageSwitcher,但我面临着同样的问题。任何帮助将不胜感激。

最佳答案

imgswitcher.setFactory(this);

不见了

编辑问题作者给出了进一步的解决方案:

public MyActivity implements ViewSwitcher.ViewFactory

@Override
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setLayoutParams(
new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
return imageView;
}

除了使用 setFactory 设置工厂外,还有其他选择。阅读 ViewSwitcher docs .

关于android - IMageSwitcher.setImageResource() 中的空点异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19950344/

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