我想实现我的应用程序 PagerSlidingTabStrip 。我做的一切都是我应该做的:
添加到我的XML:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< android.support.v4.view.ViewPager
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /寻呼机
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
< com.astuetz.PagerSlidingTabStrip
机器人:ID =@ + ID / slidingTabs
机器人:layout_width =match_parent
机器人: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):致命异常:主要
04-17 20:56:55.956:E / AndroidRuntime(1587):java.lang.RuntimeException的:无法启动的活动ComponentInfo{com.matejhacin.beautifulvoicerecorder/com.matejhacin.beautifulvoicerecorder.MainActivity}:显示java.lang.NullPointerException
04-17 20:56:55.956:E / AndroidRuntime(1587):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-17 20:56:55.956:E / AndroidRuntime(1587):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-17 20:56:55.956:E / AndroidRuntime(1587):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
04-17 20:56:55.956:E / AndroidRuntime(1587):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256)
04-17 20:56:55.956:E / AndroidRuntime(1587):在android.os.Handler.dispatchMessage(Handler.java:99)
04-17 20:56:55.956:E / AndroidRuntime(1587):在android.os.Looper.loop(Looper.java:137)
04-17 20:56:55.956:E / AndroidRuntime(1587):在android.app.ActivityThread.main(ActivityThread.java:5103)
04-17 20:56:55.956:E / AndroidRuntime(1587):在java.lang.reflect.Method.invokeNative(本机方法)
04-17 20:56:55.956:E / AndroidRuntime(1587):在java.lang.reflect.Method.invoke(Method.java:525)
04-17 20:56:55.956:E / AndroidRuntime(1587):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
04-17 20:56:55.956:E / AndroidRuntime(1587):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-17 20:56:55.956:E / AndroidRuntime(1587):在dalvik.system.NativeStart.main(本机方法)
04-17 20:56:55.956:E / AndroidRuntime(1587):由:显示java.lang.NullPointerException
04-17 20:56:55.956:E / AndroidRuntime(1587):在com.astuetz.PagerSlidingTabStrip.notifyDataSetChanged(PagerSlidingTabStrip.java:200)
04-17 20:56:55.956:E / AndroidRuntime(1587):在com.astuetz.PagerSlidingTabStrip.setViewPager(PagerSlidingTabStrip.java:182)
04-17 20:56:55.956:E / AndroidRuntime(1587):在com.matejhacin.beautifulvoicerecorder.MainActivity.onCreate(MainActivity.java:39)
04-17 20:56:55.956:E / AndroidRuntime(1587):在android.app.Activity.performCreate(Activity.java:5133)
04-17 20:56:55.956:E / AndroidRuntime(1587):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-17 20:56:55.956:E / AndroidRuntime(1587):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
解决方案
该错误未在此库处理的很好。如果你看一下PagerAdapter的来源,你会看到一个方法getPageTitle: https://github.com/android/platform_frameworks_support/blob/master/v4/java/android/support/v4/view/PagerAdapter.java#L306-Lundefined
请注意,默认情况下它返回null。这就是为什么你看到一个空指针异常。解决的办法是在实现PagerAdapter的覆盖此方法
@覆盖
公共CharSequence的getPageTitle(INT位置){
//返回选项卡的标题
}
如果您不实的想串题,并且要在标签使用自定义的视图,如图像,那么你的PagerAdapter必须实现PagerSlidingTabStrip.TabCustomViewProvider并覆盖getPageTabCustomView
@覆盖
公共查看getPageTabCustomView(INT位置){
//返回自定义视图
}
编辑:为了使用自定义选项卡视图,您将需要使用此叉: https://github.com/astuetz/PagerSlidingTabStrip/pull/32
I'm trying to implement PagerSlidingTabStrip in my app. I did everything I'm supposed to do:
Added it to my 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>
Created PagerSlidingTabStrip and tried to set ViewPager:
PagerSlidingTabStrip slidingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.slidingTabs);
slidingTabStrip.setViewPager(vPager);
But when I run the app, it crashes and this is the logcat output:
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)
解决方案
The error isn't handled nicely in this library. If you look at the source of PagerAdapter you will see a method getPageTitle: https://github.com/android/platform_frameworks_support/blob/master/v4/java/android/support/v4/view/PagerAdapter.java#L306-Lundefined
Notice that by default it returns null. This is why you are seeing a null pointer exception. The solution is to override this method in your implementation of PagerAdapter
@Override
public CharSequence getPageTitle(int position) {
// return the title of the tab
}
If you don't actually want a string title, and you want the tab to use a custom view, such as an image, then your PagerAdapter has to implement PagerSlidingTabStrip.TabCustomViewProvider and override getPageTabCustomView
@Override
public View getPageTabCustomView(int position) {
// return a custom view
}
EDIT: In order to use the custom tab view, you will need to use this fork: https://github.com/astuetz/PagerSlidingTabStrip/pull/32
相关推荐
最新文章