2014-04-17 95 views
3

我試圖在我的應用程序中實現PagerSlidingTabStrip。我做的一切都是我應該做的事:PagerSlidingTabStrip NullPointerException當設置ViewPager

把它添加到我的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) 

回答

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 
} 

編輯:爲了使用自定義選項卡視圖,您將需要使用此叉: https://github.com/astuetz/PagerSlidingTabStrip/pull/32

1
Caused by: java.lang.NullPointerException 
at com.astuetz.PagerSlidingTabStrip.notifyDataSetChanged(PagerSlidingTabStrip.java:200) 

你在line 200 in PagerSlidingTabStrip上投擲NullPointerException

這表示PagerAdapter.getPageTitle正在返回null

+0

是的,但究竟是什麼意思?我從來沒有使用過這個庫,我不知道如何解決這個問題。 – Guy

+0

@Whiz與圖書館很少有關係。 [你需要綁定一個'PagerAdapter'到你的'ViewPager'](https://github.com/astuetz/PagerSlidingTabStrip/blob/master/sample/src/com/astuetz/viewpager/extensions/sample/MainActivity.java# L59)和[覆蓋'PagerAdpater.getPageTitle'](https://github.com/astuetz/PagerSlidingTabStrip/blob/master/sample/src/com/astuetz/viewpager/extensions/sample/MainActivity.java#L186)做。 – adneal

0

你應該重寫你的適配器getPageTitle方法,因爲它返回null

@覆蓋
公共CharSequence的getPageTitle(INT位置)
{

回報tabTitles [位置];
}

+0

你能告訴我如何使用這個lib在每個選項卡中添加一個自定義視圖?我想用文字+圖標替換文字。 –

+0

我希望這篇文章能夠幫助你解決你的問題.http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/ –

相關問題