问题
I am doing an animation to show many images in ImageSwitcher
but am stuck in setting images to ImageSwitcher
.
This is the imageswitcher in my 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>
and here is my code
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);
I am getting error on line "imgswitcher.setImageResource(imgs[index]);
"
Error
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)
I even tried it without the Runnable
just assigning a drawable to ImageSwitcher
, but am facing the same issue. Any help would be appreciated.
回答1:
imgswitcher.setFactory(this);
is missing
Edit Further solution is given by question author:
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;
}
There are alternatives to setting a factory with setFactory
. Read the ViewSwitcher docs.
回答2:
I hope it helps.
Override onViewCreated Method and put your ImageSwitcher inside it. i.e.
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mImageSwitcher.setImageResource(image[index]);
}
回答3:
that is because you need to call imageSwitcher.setFacotry on the imageSwitcher before any call to imageSwitcher.setImageResource , just use the code below :
imageSwitcher.setFactory(new ViewFactory() {
public View makeView() {
ImageView myView = new ImageView(getApplicationContext());
return myView;
}
}
//then you can call setImageResource
imageSwitcher.setImageResource(R.drawable.my_image);
来源:https://stackoverflow.com/questions/19950344/null-point-exception-in-imageswitcher-setimageresource