5

我正在尝试设置一个FragmentStatePagerAdapter以在我的一个片段上创建一个可滚动的选项卡界面,类似于此示例。在我的活动中onCreate

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.base);

     setVersionString();
     setupActionBar();
     setupNavigationDrawer();
     setupFragments();
}

我将内容视图设置为以下布局 ( R.layout.base):

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BaseActivity" >

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/main_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/nav_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#FFF" />

</android.support.v4.widget.DrawerLayout>

然后我调用以下函数:

private void setupFragments() {
    // Check that the activity is using the correct layout
    if (findViewById(R.id.main_frame) != null) {
        // Initialize the first fragment
        MainFragment firstFrag = new MainFragment();

        // Pass any extras from an intent to the Fragment
        firstFrag.setArguments(getIntent().getExtras());

        // Add the first Fragment to the screen
        getSupportFragmentManager().beginTransaction()
            .add(R.id.main_frame, firstFrag).commit();

        tabsAdapter = firstFrag.getAdapter();
        Log.d("Base.LOG", "tabsAdapter = " + tabsAdapter);
        tabsAdapter.addTab(DummyFragment.class, null);
    }
}

尝试将 a 设置FragmentStatePagerAdapter为片段布局 ( ) 中视图的适配器R.layout.main

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_page"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainFragment" >

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>

我的片段的代码看起来是这样的:

private View mView;
private TabbedPagerAdapter mTabPageAdapter;
private ViewPager mTabPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.d("Main.LOG", "onCreate called");
    mView = inflater.inflate(R.layout.main, container, false);
    Log.d("Main.LOG", "Layout inflated");
    setupTabs();
    Log.d("Main.LOG", "Tabs setup");

    return mView;
}

@Override
public void onResume() {
    super.onResume();
    Log.d("Main.LOG", "onResume called");
}

/** Set up the ViewPager and Adapter to handle the primary tabs */
private void setupTabs() {
    // Initialize the adapter
    mTabPageAdapter = new TabbedPagerAdapter(getActivity(),
            getActivity().getSupportFragmentManager());

    // Initialize the view pager
    mTabPager = (ViewPager) mView.findViewById(R.id.main_page);
    Log.d("Main.LOG", "mTabPager = " + mTabPager);
    Log.d("Main.LOG", "mTabPageAdapter = " + mTabPageAdapter);
    mTabPager.setAdapter(mTabPageAdapter);
}

/* Accessor for TabbedPagerAdapter */
public TabbedPagerAdapter getAdapter() {
    return mTabPageAdapter;
}

当我尝试添加带有NullPointerException. 日志中的调试消息显示:

D/Base.LOG(27173): tabsAdapter = null

显示在任何片段的调试消息之前。在尝试访问它的任何视图或子类之前,如何确保布局已膨胀?

4

2 回答 2

2

根据我的评论

public class MainFragment extends Fragment
{
    private MainInterface mInterface = null;
    //make sure to set the above somehow

    public interface MainInterface{
        public void onFragmentReady(.....);
    }

    ... //Do regular stuff here

    public void onResume()
    {
        //Do what you need to do in onResume
        if(mInterface != null)
        {
            mInteface.onFragmentReady();
        }
    }
}

这应该让您知道片段何时准备就绪,然后您可以进行 getTabAdapter 调用。

于 2013-09-05T15:51:54.907 回答
1

您在调用之前是否在 ActivitysetContentView(...)的方法中调用?onCreate(...)setupFragments(...)

如果是:您分配给 Activity 的布局是否包含 ViewPager?

我的建议:由于您显然在 Fragment 中使用 ViewPager,因此请在 Fragments方法中对ViewPager进行初始化。onCreateView(...)

例子:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // ASSUMING your layout.main contains the ViewPager
    View v = inflater.inflate(R.layout.main, container, false);

    ViewPager pager = (ViewPager) v.findViewById(R.id.pager);
    // and so on ...

    return v;
}
于 2013-09-04T21:44:20.570 回答