我正在尝试在我的应用中实施PagerSlidingTabStrip。我做了我应该做的一切:
将其添加到我的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/slidingTabs"
android:layout_width="match_parent"
android:layout_height="48dip" />
</RelativeLayout>
创建了PagerSlidingTabStrip并尝试设置ViewPager:
PagerSlidingTabStrip slidingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.slidingTabs);
slidingTabStrip.setViewPager(vPager);
但是当我运行应用程序时,它会崩溃并且这是logcat输出:
04-17 20:56:55.956: E/AndroidRuntime(1587): FATAL EXCEPTION: main
04-17 20:56:55.956: E/AndroidRuntime(1587): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matejhacin.beautifulvoicerecorder/com.matejhacin.beautifulvoicerecorder.MainActivity}: java.lang.NullPointerException
04-17 20:56:55.956: E/AndroidRuntime(1587): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-17 20:56:55.956: E/AndroidRuntime(1587): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-17 20:56:55.956: E/AndroidRuntime(1587): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-17 20:56:55.956: E/AndroidRuntime(1587): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-17 20:56:55.956: E/AndroidRuntime(1587): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 20:56:55.956: E/AndroidRuntime(1587): at android.os.Looper.loop(Looper.java:137)
04-17 20:56:55.956: E/AndroidRuntime(1587): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-17 20:56:55.956: E/AndroidRuntime(1587): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 20:56:55.956: E/AndroidRuntime(1587): at java.lang.reflect.Method.invoke(Method.java:525)
04-17 20:56:55.956: E/AndroidRuntime(1587): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-17 20:56:55.956: E/AndroidRuntime(1587): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-17 20:56:55.956: E/AndroidRuntime(1587): at dalvik.system.NativeStart.main(Native Method)
04-17 20:56:55.956: E/AndroidRuntime(1587): Caused by: java.lang.NullPointerException
04-17 20:56:55.956: E/AndroidRuntime(1587): at com.astuetz.PagerSlidingTabStrip.notifyDataSetChanged(PagerSlidingTabStrip.java:200)
04-17 20:56:55.956: E/AndroidRuntime(1587): at com.astuetz.PagerSlidingTabStrip.setViewPager(PagerSlidingTabStrip.java:182)
04-17 20:56:55.956: E/AndroidRuntime(1587): at com.matejhacin.beautifulvoicerecorder.MainActivity.onCreate(MainActivity.java:39)
04-17 20:56:55.956: E/AndroidRuntime(1587): at android.app.Activity.performCreate(Activity.java:5133)
04-17 20:56:55.956: E/AndroidRuntime(1587): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-17 20:56:55.956: E/AndroidRuntime(1587): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
答案 0 :(得分:10)
此库中的错误处理不当。如果你看一下PagerAdapter的来源,你会看到一个方法getPageTitle: https://github.com/android/platform_frameworks_support/blob/master/v4/java/android/support/v4/view/PagerAdapter.java#L306-Lundefined
请注意,默认情况下它返回null。这就是您看到空指针异常的原因。解决方案是在PagerAdapter
的实现中覆盖此方法@Override
public CharSequence getPageTitle(int position) {
// return the title of the tab
}
如果您实际上并不想要字符串标题,并且希望标签使用自定义视图(例如图像),那么您的PagerAdapter必须实现PagerSlidingTabStrip.TabCustomViewProvider并覆盖getPageTabCustomView
@Override
public View getPageTabCustomView(int position) {
// return a custom view
}
编辑:要使用自定义选项卡视图,您需要使用此fork: https://github.com/astuetz/PagerSlidingTabStrip/pull/32
答案 1 :(得分:1)
Caused by: java.lang.NullPointerException
at com.astuetz.PagerSlidingTabStrip.notifyDataSetChanged(PagerSlidingTabStrip.java:200)
你在line 200
in PagerSlidingTabStrip
上投了NullPointerException
。
这表示PagerAdapter.getPageTitle
正在返回null
。
答案 2 :(得分:0)
您应该覆盖适配器getPageTitle方法,因为它返回null
@覆盖
public CharSequence getPageTitle(int position)
{
返回tabTitles [position];
}