FragmentStatePagerAdapter
this exampleのような私のフラグメントの1つにスクロール可能なタブインターフェイスを作成するように設定しようとしています。私の活動のonCreate
で:する試みでFragmentのビューを取得するためにfindViewById()を呼び出すとき
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);
}
}
:私は、次の関数を呼び出す
<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>
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base);
setVersionString();
setupActionBar();
setupNavigationDrawer();
setupFragments();
}
私は次のようなレイアウト(R.layout.base
)へのコンテンツビューを設定しますフラグメントのレイアウト内のビューのアダプタとして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
が断片のデバッグ・メッセージのいずれかの前に表示されるログショーでデバッグメッセージ。ビューやサブクラスにアクセスしようとする前に、レイアウトが膨張していることを確認するにはどうすればよいですか?
R.layout.mainのxmlを表示できますか?インフレーションが完了したかどうかを知りたければ、onResumeをフラグメントに入れ、インフレーションが完了したかどうかを確認してください。なぜならonResumeは常にonCreateViewの後に呼び出されるからです。 – Raigex
@Raigexそれは第2のものです。私はどちらを含むかを編集しました。私は 'onResume'メソッドで何をするとお勧めしますか? –
本当に何もありませんが、再開メソッドを使用して、膨らんでいるかどうかを確認することができます(例:fragView!= null)。次にログに記録して問題を絞り込むことができます。 – Raigex