在另一个片段内创建Tab布局会引发异常

时间:2016-05-20 06:57:49

标签: java android android-fragments

尝试创建一个TabLayout,其中包含三个标签(片段) 另一个片段。

现在当我执行代码时,这是我在logcat中获得的:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout.newTab()' on a null object reference
                      at nanodegree.premiere.adapter.ListFragment.onCreateView(ListFragment.java:28)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1226)
                      at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1328)
                      at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2284)
                      at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
                      at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
                      at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                      at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                      at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                      at nanodegree.premiere.Activities.TabActivity.onCreate(TabActivity.java:13) 
                      at android.app.Activity.performCreate(Activity.java:6237) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

现在,此文件包含创建选项卡布局的两个片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

<fragment
    class="nanodegree.premiere.adapter.ListFragment"
    android:id="@+id/mainFragment"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".Activities.MainActivity">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="?attr/colorPrimary"
            android:elevation="6dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout_tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar_tab"
            android:background="?attr/colorPrimary"
            android:elevation="6dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/pager_tab"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/tab_layout_tab"/>
    </RelativeLayout>
</fragment>

<fragment
    class="nanodegree.premiere.adapter.DetailFragment"
    android:id="@+id/detailFragment"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent"
    >
</fragment>

该布局文件的相应Java代码:

public class TabActivity extends AppCompatActivity {

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

现在,这个Fragment类是我获得异常的地方。这也是我创建标签式布局的地方:

   import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import nanodegree.premiere.R;

public class ListFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.list_fragment_tab, container, false);
        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar_tab);
        //((AppCompatActivity)getActivity()).getSupportActionBar(toolbar);

        TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tab_layout_tab);
        tabLayout.addTab(tabLayout.newTab().setText("Popular"));
        tabLayout.addTab(tabLayout.newTab().setText("Top rated"));
        tabLayout.addTab(tabLayout.newTab().setText("Favorites"));

        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.pager);
        final TabsPagerAdapter adapter = new TabsPagerAdapter
                (getFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(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) {

            }
        });

        return rootView;
    }
}

如何解决这个问题?

0 个答案:

没有答案