我使用的是viewpager,將獲得的圖像源從一個Web服務,並動態頁面添加到viewpager。如何在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>
這裏的instantiateItem從ViewPager類:
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)
任何人都知道我可以嘗試什麼? 我可以看到它是某種度量問題,但我不知道在這個問題上該怎麼做。
在此先感謝
非常感謝!當然我沒有看到那個錯字..... – Ikky