Android:更改图像滑动视图

时间:2013-07-31 18:51:46

标签: java android imageview

我想创建一个包含几页的swipe view

它们都有相同的布局,但我想在每个页面上显示其他内容, 内置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)

我很抱歉复制并粘贴我的日志...

但我找不到另一种改变drawable的方法。 Eclipse没有显示任何错误......

:( 里奇

1 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案。

在我的xml文件中,我必须使用

id="@android:id/icon"

而不是

id="@+id/image1"