TabLayout 中的标题不显示

Titles in TabLayout donsen't appear

伙计们,我在 Android 中遇到了麻烦,我的代码可以编译但不能正常工作! 我尝试用 ViewPager 做一个 TabLayout,但是文本没有出现。

这是代码的结构:

我基于 Android Studio 创建的 DrawableLayout 构建它,所以让我们检查我的代码:

主要Activity:

库已全部导入。

import android.support.design.widget.TabLayout;

// Tabs and ViewPager
    TabLayout tabLayout = findViewById(R.id.tabs);
    ViewPager viewPager = findViewById(R.id.viewpager);

    TabsPager tabsPager = new TabsPager(getSupportFragmentManager());
    viewPager.setAdapter(tabsPager);
    tabLayout.setupWithViewPager(viewPager);

TabsPager 是我的 Java 项目中的另一个 class

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class TabsPager extends FragmentStatePagerAdapter {
  String[] titles= new String[]{"Trending Now", "Topics", "Kids Zone"};

 public TabsPager(FragmentManager fm) {
    super(fm);
 }

 @Override
 public CharSequence getPageTitle(int position) {
     return titles[position];
 }

 @Override
 public Fragment getItem(int position) {
     switch (position) {
        case 0:
            TrendingNow trendingNow = new TrendingNow();
            return new TrendingNow();
        case 1:
            Topics topics = new Topics();
            return new Topics();
        case 2:
            Kids kids = new Kids();
            return new Kids();
    }
    return null;
 }

 @Override
 public int getCount() {
     return 0;
 }
}

我还创建了 3 个 Fragments Class 还有 XML 代码

最后在我的 content_main.xml 中添加:

<android.support.v4.view.ViewPager
       android:id="@+id/viewpager"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

问题出在哪里??

亚行Shell结果:

$ adb shell am start -n "com.squarcy.equals2/com.squarcy.equals2.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 28856 on device umidigi-g-0123456789ABCDEF
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/System: ClassLoader referenced unknown path: /data/app/com.squarcy.equals2-1/lib/arm
I/art: Enter while loop.
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
V/PhoneWindow: DecorView setVisiblity: visibility = 4, Parent = null, this = DecorView@a069b0b[]
D/WindowClient: Add to mViews: DecorView@a069b0b[MainActivity], this = android.view.WindowManagerGlobal@764b2af
D/OpenGLRenderer: Dumper init 2 threads <0xa57e5500>
D/OpenGLRenderer: <com.squarcy.equals2> is running.
D/OpenGLRenderer: CanvasContext() 0xa8463000
D/GraphicBuffer: register, handle(0xae7b4ac0) (w:960 h:576 s:960 f:0x1 u:0x000100)
D/ViewRootImpl[MainActivity]: hardware acceleration is enabled, this = ViewRoot{6ab96bc com.squarcy.equals2/com.squarcy.equals2.MainActivity,ident = 0}
V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = ViewRoot{6ab96bc com.squarcy.equals2/com.squarcy.equals2.MainActivity,ident = 0}, this = DecorView@a069b0b[MainActivity]
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.squarcy.equals2, PID: 28856
                  java.lang.RuntimeException: com.squarcy.equals2.MainActivity@a398dfc must implement OnFragmentInteractionListener
                      at com.squarcy.equals2.Topics.onAttach(Topics.java:83)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1372)
                      at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1188)
                      at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1071)
                      at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:115)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2380)
                      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
                      at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2215)
                      at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:649)
                      at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1238)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
                      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1616)
                      at android.view.View.measure(View.java:20084)
                      at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1210)
                      at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1550)
                      at android.view.View.measure(View.java:20084)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
                      at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:739)
                      at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:91)
                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1361)
                      at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:809)
                      at android.view.View.measure(View.java:20084)
                      at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1059)
                      at android.view.View.measure(View.java:20084)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
                      at android.view.View.measure(View.java:20084)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1705)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:797)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
                      at android.view.View.measure(View.java:20084)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
                      at android.view.View.measure(View.java:20084)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1705)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:797)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
                      at android.view.View.measure(View.java:20084)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6282)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
                      at com.android.internal.policy.DecorView.onMeasure(DecorView.java:714)
                      at android.view.View.measure(View.java:20084)
                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2640)
                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1577)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1846)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6965)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:709)
                      at android.view.Choreographer.doFrame(Choreographer.java:644)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893)
                      at android.os.Handler.handleCallback(Handler.java:836)
                      at android.os.Handler.dispatchMessage(Handler.java:103)
                      at android.os.Looper.loop(Looper.java:203)
                      at android.app.ActivityThread.main(ActivityThread.java:6251)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Application terminated.

您的代码:

@Override
 public int getCount() {
     return 0;
 }

替换为:

@Override
        public int getCount() {
            return titles.size();
        }