ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-07-25 11:24:45  阅读:348  来源: 互联网

标签:android drawable android-image imageswitcher


我正在做一个动画来在ImageSwitcher中显示许多图像,但我在设置图像时陷入了ImageSwitcher.

这是我的xml中的imageswitcher.

<?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只为ImageSwitcher分配一个drawable,但我遇到了同样的问题.任何帮助,将不胜感激.

解决方法:

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,drawable,android-image,imageswitcher
来源: https://codeday.me/bug/20190725/1532457.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有