2013-07-31 99 views
1

我想用幾頁創建一個swipe viewAndroid:更改圖像滑動視圖

它們都具有相同的佈局,但我希望在每個頁面上顯示其他內容,其中包含ImageViews和TextViews,內部爲 。

在每次滑動時,我必須更改文本內容或圖像的drawable來顯示我的不同頁面。

於是我就用

((TextView) rootView.findViewById(R.id.text1)) 
     .setText(R.string.my_text1); 

這個工程..

但這:

((ImageView) rootView.findViewById(R.id.image1)) 
     .setImageResource(R.drawable.pic1); 

或本:

((ImageView) rootView.findViewById(R.id.image1)) 
     .setImageDrawable(getResources().getDrawable(R.drawable.pic1); 

沒有。

應用崩潰,和logcat的說:

07-31 18:31:41.467: E/AndroidRuntime(792): FATAL EXCEPTION: main 
07-31 18:31:41.467: E/AndroidRuntime(792): java.lang.NullPointerException 
07-31 18:31:41.467: E/AndroidRuntime(792): at com.keksarmee.mcguide.Friendly$ObjectFragment.onCreateView(Friendly.java:119) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)  
07-31 18:31:41.467: E/AndroidRuntime(792): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.support.v4.view.ViewPager.populate(ViewPager.java:1064) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.support.v4.view.ViewPager.populate(ViewPager.java:911) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.View.measure(View.java:15518) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.View.measure(View.java:15518) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.View.measure(View.java:15518) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
07-31 18:31:41.467: E/AndroidRuntime(792): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.View.measure(View.java:15518) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.Choreographer.doFrame(Choreographer.java:532) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.os.Handler.handleCallback(Handler.java:725) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.os.Handler.dispatchMessage(Handler.java:92) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.os.Looper.loop(Looper.java:137) 
07-31 18:31:41.467: E/AndroidRuntime(792): at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-31 18:31:41.467: E/AndroidRuntime(792): at java.lang.reflect.Method.invokeNative(Native Method) 
07-31 18:31:41.467: E/AndroidRuntime(792): at java.lang.reflect.Method.invoke(Method.java:511) 
07-31 18:31:41.467: E/AndroidRuntime(792): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-31 18:31:41.467: E/AndroidRuntime(792): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-31 18:31:41.467: E/AndroidRuntime(792): at dalvik.system.NativeStart.main(Native Method) 

對不起拷貝粘貼&我的日誌...

但我無法找到另一種方式來改變繪製。 Eclipse中沒有顯示任何錯誤......

:( 裏奇

+0

java.lang.NullPointerException,所以必須有一個方法在某處調用一個'null'值。你可以運行一些測試來查看哪個對象是'null'?很可能某個對象不存在或者已經被忘記初始化。 – bas

+0

我目前的佈局是page_passive.xml,但ImageView在頁面page_passive_fragment_collection_object.xml中,所以也許這樣:'((ImageView)rootView.findViewById(R.id.image1))'爲空。 (看到它[這裏](http://stackoverflow.com/questions/4393976/android-imageview-nullpointerexception)) – KeksArmee

回答

1

OK,我已經找到了解決辦法。

在我的XML文件,我不得不使用

id="@android:id/icon" 

而不是

id="@+id/image1"