这是有效的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.astuetz.viewpager.extensions.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip" >
</com.astuetz.viewpager.extensions.PagerSlidingTabStrip>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:context=".MainActivity" >
</android.support.v4.view.ViewPager>
</LinearLayout>
这里是我的 Activity OnCreate 方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
tabs.setDividerColor(Color.BLUE);
tabs.setBackgroundColor(Color.DKGRAY);
tabs.setTextColor(Color.WHITE);
tabs.setIndicatorColor(Color.GREEN);
}
如果在布局中我反转 PagerSlidingTabStrip 和 ViewPager,项目将在启动时崩溃,而在第一个实现中,它会正常工作。
这是错误:
05-19 16:50:11.636: E/AndroidRuntime(305): FATAL EXCEPTION: main
05-19 16:50:11.636: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hsware.peeem/com.hsware.peeem.MainActivity}: java.lang.ClassCastException: android.support.v4.view.ViewPager
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.os.Looper.loop(Looper.java:123)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-19 16:50:11.636: E/AndroidRuntime(305): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 16:50:11.636: E/AndroidRuntime(305): at java.lang.reflect.Method.invoke(Method.java:521)
05-19 16:50:11.636: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-19 16:50:11.636: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-19 16:50:11.636: E/AndroidRuntime(305): at dalvik.system.NativeStart.main(Native Method)
05-19 16:50:11.636: E/AndroidRuntime(305): Caused by: java.lang.ClassCastException: android.support.v4.view.ViewPager
05-19 16:50:11.636: E/AndroidRuntime(305): at com.hsware.peeem.MainActivity.onCreate(MainActivity.java:41)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-19 16:50:11.636: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
看来它无法检索 de R.id.pager,我不确定。