Home>

I wanted to make tabs with this answer... I copied everything and launched it. The first time you load a snippet that has tabs in it, everything is fine. Only when I restart the snippet (change the theme of the application, go to the second page) everything suddenly disappears and an error appears in the console stating that MainActivity cannot find the views and change the text. I have no idea why this is happening. Help me please.

fragment_user.xml:

<
TabHost
      android: id= "@ android: id /tabhost"
      android: layout_width= "0dp"
      android: layout_height= "0dp"
      android: layout_marginTop= "8dp"
      app: layout_constraintBottom_toBottomOf= "parent"
      app: layout_constraintEnd_toEndOf= "parent"
      app: layout_constraintStart_toStartOf= "parent"
      app: layout_constraintTop_toBottomOf= "@ + id /titleProfile" >
    <
LinearLayout
        android: layout_width= "match_parent"
        android: layout_height= "match_parent"
        android: orientation= "vertical" >
      <
TabWidget
          android: id= "@ android: id /tabs"
          android: layout_width= "match_parent"
          android: layout_height= "wrap_content"
          android: layout_weight= "0"
          android: orientation= "horizontal" />
      <
FrameLayout
          android: id= "@ android: id /tabcontent"
          android: layout_width= "0dp"
          android: layout_height= "0dp"
          android: layout_weight= "0" />
      <
androidx.viewpager.widget.ViewPager
          android: id= "@ + id /pager"
          android: layout_width= "match_parent"
          android: layout_height= "0dp"
          android: layout_weight= "1" />
    <
/LinearLayout >
  <
/TabHost >

MainActivity.java:

try {
    TextView orderText= findViewById (R.id.orderText);
    TextView datecreated= findViewById (R.id.orderDateCreated);
    datecreated.setText (...);
    orderText.setText (...);
    findViewById (R.id.noneOrdersFrame) .setVisibility (View.INVISIBLE);
    findViewById (R.id.yourOrdersText) .setVisibility (View.VISIBLE);
    findViewById (R.id.orderFrame) .setVisibility (View.VISIBLE);
    findViewById (R.id.profileLoadingTxt) .setVisibility (View.INVISIBLE);
} catch (Exception e) {
    Log.e ("error", e.toString ());
}

UserFragment.java:

private TabHost mTabHost;
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
public UserFragment () {}
@Override
public void onCreate (Bundle instance) {
  super.onCreate (instance);
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
  View v= inflater.inflate (R.layout.fragment_user, container, false);
  mTabHost= v.findViewById (android.R.id.tabhost);
  mTabHost.setup ();
  mViewPager= v.findViewById (R.id.pager);
  mTabsAdapter= new TabsAdapter (getActivity (), mTabHost, mViewPager);
  mTabsAdapter.addTab (mTabHost.newTabSpec ("one"). setIndicator ("My order"), TabOrder.class, null);
  mTabsAdapter.addTab (mTabHost.newTabSpec ("two"). setIndicator ("History"), TabHistory.class, null);
  return v;
}
public static class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
  private final Context mContext;
  private final TabHost mTabHost;
  private final ViewPager mViewPager;
  private final ArrayList <
TabInfo >
 mTabs= new ArrayList <
TabInfo >
();
  static final class TabInfo {
    private final String tag;
    private final Class <
? >
 clss;
    private final Bundle args;
    TabInfo (String _tag, Class <
? >
 _class, Bundle _args) {
      tag= _tag;
      clss= _class;args= _args; }
  }
  static class DummyTabFactory implements TabHost.TabContentFactory {
    private final Context mContext;
    public DummyTabFactory (Context context)
    {
      mContext= context;
    }
    public View createTabContent (String tag) {
      View v= new View (mContext);
      v.setMinimumWidth (0);
      v.setMinimumHeight (0);
      return v;
    }
  }
  public TabsAdapter (FragmentActivity activity, TabHost tabHost, ViewPager pager) {
    super (activity.getSupportFragmentManager ());
    mContext= activity;
    mTabHost= tabHost;
    mViewPager= pager;
    mTabHost.setOnTabChangedListener (this);
    mViewPager.setAdapter (this);
    mViewPager.setOnPageChangeListener (this);
  }
  public void addTab (TabHost.TabSpec tabSpec, Class <
? >
 clss, Bundle args) {
    tabSpec.setContent (new DummyTabFactory (mContext));
    String tag= tabSpec.getTag ();
    TabInfo info= new TabInfo (tag, clss, args);
    mTabs.add (info);
    mTabHost.addTab (tabSpec);
    notifyDataSetChanged ();
  }
  @Override
  public int getCount ()
  {
    return mTabs.size ();
  }
  @Override
  public Fragment getItem (int position) {
    TabInfo info= mTabs.get (position);
    return Fragment.instantiate (mContext, info.clss.getName (), info.args);
  }
  public void onTabChanged (String tabId) {
    int position= mTabHost.getCurrentTab ();
    mViewPager.setCurrentItem (position);
  }
  public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {}
  public void onPageSelected (int position) {
    TabWidget widget= mTabHost.getTabWidget ();
    int oldFocusability= widget.getDescendantFocusability ();
    widget.setDescendantFocusability (ViewGroup.FOCUS_BLOCK_DESCENDANTS);
    mTabHost.setCurrentTab (position);
    widget.setDescendantFocusability (oldFocusability);
  }
  public void onPageScrollStateChanged (int state) {}
}

TabOrder.java:

public View onCreateView (@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    View root= inflater.inflate (R.layout.tab_order, container, false);
    return root;
}

Give the code for Mainactivity. And make the data declaration code in onResume

Arty Morris2022-01-04 01:33:12

@ArtyMorris Sorry, but I didn't quite understand what needs to be done. And I also had a question, how can I find the view in the viewpager. Please edit my code and post a response. If everything works out, then I'll mark it as an answer.

Oleg Pruh2022-01-03 10:04:38