使用本教程:http ://www.lucazanini.eu/2012/android/swipe-views/?lang= en,我现在可以基于几个 xml 文件创建滑动页面。
假设第一页是 tab1.xml,它包含:
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this is my button"
android:onClick="myButton_onClick" />
第二页是tab2.xml,它有:
<ImageView
android:id="@+id/myImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/something" />
现在我的应用程序可以从一个页面滑动到另一个页面,这正是我想要的。但是,当我尝试控制 MainActivity.java 上的<Button>
and<ImageView>
时,它总是给我 nullpointerexception 错误。
然后我意识到这两者<Button>
都不<ImageView>
是activity_main.xml的一部分,所以它可能无法从MainActivity.java访问。实际上activity_main.xml 只包含每个可滑动页面的标题。现在我什至不知道将按钮的 onClick 事件放在哪里。
我希望一些android专家能给我一个线索。
日志:
11-05-21:48:50.061: D/ActivityThread(13482): setTargetHeapUtilization:0.25
11-05-21:48:50.061: D/ActivityThread(13482): setTargetHeapIdealFree:8388608
11-05-21:48:50.061: D/ActivityThread(13482): setTargetHeapConcurrentStart:2097152
11-05-21:48:50.251: W/dalvikvm(13482): threadid=1: thread exiting with uncaught exception (group=0x40c5e498)
11-05-21:48:50.251: E/AndroidRuntime(13482): FATAL EXCEPTION: main
11-05-21:48:50.251: E/AndroidRuntime(13482): java.lang.NullPointerException
11-05-21:48:50.251: E/AndroidRuntime(13482): at com.anggrian.speaknow.MainActivity$TabFragment.onCreateView(MainActivity.java:152)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.View.measure(View.java:15247)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4834)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.View.measure(View.java:15247)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.View.measure(View.java:15247)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4834)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-05-21:48:50.251: E/AndroidRuntime(13482): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.View.measure(View.java:15247)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1881)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1129)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1306)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1027)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4359)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.Choreographer.doFrame(Choreographer.java:525)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.os.Handler.handleCallback(Handler.java:615)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.os.Handler.dispatchMessage(Handler.java:92)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.os.Looper.loop(Looper.java:137)
11-05-21:48:50.251: E/AndroidRuntime(13482): at android.app.ActivityThread.main(ActivityThread.java:4812)
11-05-21:48:50.251: E/AndroidRuntime(13482): at java.lang.reflect.Method.invokeNative(Native Method)
11-05-21:48:50.251: E/AndroidRuntime(13482): at java.lang.reflect.Method.invoke(Method.java:511)
11-05-21:48:50.251: E/AndroidRuntime(13482): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-05-21:48:50.251: E/AndroidRuntime(13482): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
11-05-21:48:50.251: E/AndroidRuntime(13482): at dalvik.system.NativeStart.main(Native Method)