Tôi đang cố thiết lập FragmentStatePagerAdapter
để tạo giao diện tab có thể cuộn trên một trong các đoạn của tôi tương tự như this example. Trong hoạt động của tôi onCreate
:NullPointerException khi gọi hàm findViewById() để xem Fragment's View
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base);
setVersionString();
setupActionBar();
setupNavigationDrawer();
setupFragments();
}
tôi đặt xem nội dung đến cách bố trí sau (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>
sau đó tôi gọi hàm sau:
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);
}
}
Trong một nỗ lực để đặt FragmentStatePagerAdapter
làm bộ điều hợp cho chế độ xem trong bố cục của đoạn (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>
Và đoạn mã của tôi trông dối này:
lựcprivate 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;
}
Ứng dụng đóng khi tôi cố gắng thêm một tab với một NullPointerException
. Các thông điệp debug trong chương trình ghi rằng:
D/Base.LOG (27.173): tabsAdapter = null
được hiển thị trước khi bất kỳ các thông điệp debug của đoạn. Làm thế nào tôi có thể đảm bảo rằng bố cục được tăng cao trước khi cố truy cập vào bất kỳ Chế độ xem hoặc lớp con nào?
Bạn có thể hiển thị xml cho R.layout.main không? cũng nếu bạn muốn biết nếu một cái gì đó được thực hiện lạm phát, chỉ cần đặt một onResume trong đoạn, và kiểm tra xem lạm phát được thực hiện, bởi vì onResume sẽ luôn luôn được gọi sau khi onCreateView – Raigex
@Raigex Đó là thứ hai. Tôi đã chỉnh sửa để bao gồm cái nào. Bạn đề nghị tôi làm gì trong phương thức 'onResume'? –
Không có gì thực sự, nhưng bạn có thể sử dụng phương pháp tiếp tục trên để xem nếu đã được thổi phồng (ví dụ như fragView! = Null) thì bạn có thể đăng nhập và thu hẹp vấn đề. – Raigex