我正在使用一个viewpager,它将从webservice获取图像源,并动态地将页面添加到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上遇到异常,这里是stacktrace:
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)
任何人都知道我能尝试什么? 我可以看到它是某种测量问题,但我不知道如何处理这个问题。
提前致谢
答案 0 :(得分:1)
而不是
ImageView imageView1= ((ImageView)(View)findViewById(R.id.imageView1));
使用
ImageView imageView1= ((ImageView) view.findViewById(R.id.imageView1));
答案 1 :(得分:0)
请尝试这种方式:
imageView1.setImageResource(R.drawable.button_increase);
答案 2 :(得分:0)
试试这个
View view = inflater.inflate(R.layout.test_layout, null);
ImageView imageView1= (ImageView)view.findViewById(R.id.imageView1);
imageView1.setImageResource(R.drawable.button_increase);