setVisibility 不适用于 tabLayout - TabItem returns findViewById() 的 NullPointerException

setVisibility does not work for tabLayout - TabItem returns NullPointerException for findViewById()

我在我的应用程序中添加 tabLayouttabLayout 有 3 个 table 来显示三个不同的页面,现在我尝试将一个 table 设置为 INVISIBLE,但我可以不做。

我试着这样做

tabCalls.setVisibility(View.INVISIBLE);

但这行不通

Logcat 错误..

ava.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
        at com.example.myapplication.Fragmantmarket.MainActivityFargmain.TableInVisable(MainActivityFargmain.java:137)
        at com.example.myapplication.Fragmantmarket.AboutUser.onCreateView(AboutUser.java:69)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
        at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
        at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1818)
        at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:303)
        at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:246)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
        at androidx.viewpager.widget.ViewPager.run(ViewPager.java:273)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
        at android.view.Choreographer.doCallbacks(Choreographer.java:702)
        at android.view.Choreographer.doFrame(Choreographer.java:635)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

完整代码


public class MainActivityFargmain extends AppCompatActivity {

    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    AdapterTableViewOrder adapterTableViewOrder;
    TabItem tabChats;
    TabItem tabStatus;
     TabItem tabCalls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.m_fragmant_view);

        toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle(getResources().getString(R.string.app_name));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        tabLayout = findViewById(R.id.tablayout);
        tabChats = findViewById(R.id.tabChats);
        tabStatus = findViewById(R.id.tabStatus);
        tabCalls = findViewById(R.id.tabCalls);// That I want INVISIBLE
        viewPager = findViewById(R.id.viewPager);


       tabCalls.setVisibility(View.INVISIBLE);// like that I try to do


 adapterTableViewOrder = new AdapterTableViewOrder(getSupportFragmentManager(), tabLayout.getTabCount());
       viewPager.setAdapter(adapterTableViewOrder);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    }


}

xml代码


    <com.google.android.material.tabs.TabItem
        android:id="@+id/tabChats"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Table1" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/tabStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Table2" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/tabCalls"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Table3" />

  </com.google.android.material.tabs.TabLayout>

  <androidx.viewpager.widget.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

如果有人知道错误请告诉我。

您遇到 NullPointerException 的问题是因为 TabItem 是一个特殊的小部件,无法使用 findViewById().

进行扩充

相反,在您的 activity/fragment 中给 TabItem 充气:

TabLayout tabLayout = findViewById(R.id.tablayout);

// First TabItem
View item0 = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(0);

// Second TabItem
View item0 = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(1);

注意它从索引 0 开始。所以第一个选项卡索引是 0。

要在您的代码中修复该问题:

删除这些行

TabItem tabCalls;
...
tabCalls = findViewById(R.id.tabCalls);// That I want INVISIBLE

并添加

View tabCalls;
...
// tabCalls is the third item, so we used index of 2
tabCalls = = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(2);

然后就可以正常设置可见性了

tabCalls.setVisibility(View.INVISIBLE);