我正在使用一个 viewpager,它将从 web 服务获取图像源,并将页面动态添加到 viewpager。
但现在,我只是在测试如何以编程方式更改图像。
这是简单的布局:
<?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="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:src="@drawable/button_increase" />
</LinearLayout>
这是 ViewPager 类中的 instantiateItem:
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test_layout, null);
ImageView imageView1= ((ImageView)(View)findViewById(R.id.imageView1));
imageView1.setImageDrawable(getResources().getDrawable(R.drawable.button_increase));
((ViewPager) collection).addView(view);
return view;
}
我在 setImageDrawable 上遇到异常,这是堆栈跟踪:
06-05 14:58:17.923: E/AndroidRuntime(22982): FATAL EXCEPTION: main
06-05 14:58:17.923: E/AndroidRuntime(22982): java.lang.NullPointerException
06-05 14:58:17.923: E/AndroidRuntime(22982): at *project*.MainActivity$CustomPagerAdapter.instantiateItem(MainActivity.java:93)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.view.View.measure(View.java:12863)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.view.View.measure(View.java:12863)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.view.View.measure(View.java:12863)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.view.View.measure(View.java:12863)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-05 14:58:17.923: E/AndroidRuntime(22982): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2256)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.view.View.measure(View.java:12863)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1197)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2585)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.os.Looper.loop(Looper.java:137)
06-05 14:58:17.923: E/AndroidRuntime(22982): at android.app.ActivityThread.main(ActivityThread.java:4507)
06-05 14:58:17.923: E/AndroidRuntime(22982): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 14:58:17.923: E/AndroidRuntime(22982): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 14:58:17.923: E/AndroidRuntime(22982): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-05 14:58:17.923: E/AndroidRuntime(22982): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-05 14:58:17.923: E/AndroidRuntime(22982): at dalvik.system.NativeStart.main(Native Method)
有人知道我可以尝试什么吗?我可以看到这是某种度量问题,但我不知道如何处理这个问题。
提前致谢