【问题标题】:NullPointerException is thrown when tabLayout is created [duplicate]创建 tabLayout 时抛出 NullPointerException [重复]
【发布时间】:2021-01-22 03:53:32
【问题描述】:

您好,我想在 ViewPager 类的帮助下创建 tabLayout,以便我可以在选项卡之间滑动。

我正在创建标签,我设置了 fragments、viewPagerAdapter 和 viewPager。在这里我也初始化了tabLayout,但我不明白这个错误。请帮我解决。

这是我的主要 xml,其中包含表格布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.davda.musicmain.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/toolbar_layout"
            >

            <android.support.design.widget.TabLayout
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:id="@+id/tabLayout"
                app:tabMode="scrollable"
                app:tabGravity="center">

            </android.support.design.widget.TabLayout
                >
        </include>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager"></android.support.v4.view.ViewPager>
    </RelativeLayout>

这是我的代码:

    tb = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(tb);


    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    viewPager = (ViewPager) findViewById(R.id.viewPager);

    viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

    viewPagerAdapter.addFrags(new Albums(), "Albums"); // `new Albums()` should be inside `FragmentPagerAdapter.getItem()`
    viewPagerAdapter.addFrags(new Tracks(), "Tracks"); // `new Tracks()` should be inside `FragmentPagerAdapter.getItem()`
    viewPagerAdapter.addFrags(new Artists(), "Artists"); // `new Artists()` should be inside `FragmentPagerAdapter.getItem()`

    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);

这里是堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.davda.musicmain, PID: 32662
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.davda.musicmain/com.example.davda.musicmain.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLayout.setupWithViewPager(android.support.v4.view.ViewPager)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2788)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1503)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6209)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLayout.setupWithViewPager(android.support.v4.view.ViewPager)' on a null object reference
                  at com.example.davda.musicmain.MainActivity.onCreate(MainActivity.java:35)
                  at android.app.Activity.performCreate(Activity.java:6745)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2788) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1503) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6209) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
D/AppTracker: App Event: crash
Application terminated.

【问题讨论】:

  • 请发布您的 xml
  • 好的,我正在添加 Xmls
  • 发布xml布局和onCreate方法
  • 干净构建你的apk...
  • 不喜欢干净的建筑 apk @rafsanahmad007

标签: android android-studio nullpointerexception android-viewpager android-tablayout


【解决方案1】:

只需将TabLayout 放在&lt;include&gt; 标记之外。

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar_layout"
        />

    <android.support.design.widget.TabLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/tabLayout"
        app:tabMode="scrollable"
        app:tabGravity="center"/>

</android.support.design.widget.AppBarLayout>

【讨论】:

  • No @Akash 它给了我运行时异常,你必须实现 OnFragmentInteractionListener
  • @Pedo 添加您的错误并提及您在哪一行出现错误。
  • 好的,我正在编辑问题
【解决方案2】:

请参考此代码 1. 检查 setContentView() 你的 xml 布局是否正确,你有必须放置的 viewpager、tablayout 的 xml。 2.检查标签布局的id,viewpager。

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);

}

和

  public class ViewPagerAdapter extends FragmentPagerAdapter {
      public ViewPagerAdapter(FragmentManager fragmentManager) {
          super(fragmentManager);
      }
  
      @Override
      public Fragment getItem(int position) {
          if(position == 0) return new CallLogFragment();
          if(position == 1) return new MissedCallFragment();
          if(position == 2) return new ReceivedCallFragment();
          if(position == 3) return new OutgoingCallFragment();

          throw new IllegalStateException("Unexpected position " + position);
      }
  
      @Override
      public int getCount() {
          return 4;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          if(position == 0) return "Call Log";
          if(position == 1) return "Missed Call";
          if(position == 2) return "Received Call";
          if(position == 3) return "Outgoing Call";
        
          throw new IllegalStateException("Unexpected position " + position);
      }
  }

【讨论】:

  • 不,这不是问题,这些都是正确的。
  • 你有什么问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
  • 2010-12-27
相关资源
最近更新 更多