我想用几页创建一个滑动视图。
它们都具有相同的布局,但我想在每个页面上显示其他内容,其中包含 ImageViews 和 TextViews。
每次滑动时,我都必须更改文本内容或图像的可绘制对象以显示我的不同页面。
所以我用
((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 没有显示任何错误...
:( 里奇