问题
guys I have a trouble in Android, my code compiles and but not work all fine! I try to do a TabLayout with ViewPager but the text donsen't appear.
This is the structure of the code:
I build this based on DrawableLayout own created by Android Studio, so lets check my code:
In Main Activity:
The Library is allready imported.
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 is another class in my Java Project
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;
}
}
I also created 3 Frangments Class and there XML Code
- Trending Now
- Topics
- Kids Zone
And finnaly in my content_main.xml I add this:
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Where is the problem??
ADB Shell Result:
$ 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.
回答1:
Your code:
@Override
public int getCount() {
return 0;
}
Replce with:
@Override
public int getCount() {
return titles.size();
}
来源:https://stackoverflow.com/questions/50424094/titles-in-tablayout-donsent-appear