所以我正忙着使用ViewPager
与Navigation Drawer
一起使用。我有Navigation Drawer
工作。
但是,尝试拨打viewPager.setAdapter(pagerAdapter)
会抛出NullPointerException
。
我正在检查viewPager
和pagerAdapter
是否为null,但是当我尝试Log
调用各自的toString()
方法时会发生什么:
03-11 12:31:54.156: D/My Debug Bitches(2893): android.support.v4.view.ViewPager{b1e75710 VFED.... ......I. 0,0-0,0 #7f090000 app:id/pager}
03-11 12:31:54.156: D/MyDebugBitches(2893): com.sweidenkopf.discretesuite.DataSetViewActivity$DataSetViewPagerAdapter@b1e7b4c8
请注意viewPager
toString()
如何返回一些奇怪的android.support.v4
信息?我认为这可能与这个问题有关。
以下是我的viewPager
和pagerAdapter
的初始化。我标记了导致异常的行
@Override
protected void onCreate(Bundle args) {
super.onCreate(args);
setContentView(R.layout.activity_data_set_view);
super.onCreateDrawer();
//initialize the pager adapter, giving it access to the fragment manager
pagerAdapter = new DataSetViewPagerAdapter(super.getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//initialize the view pager!
viewPager = (ViewPager) findViewById(R.id.pager);
Log.d("My Debug Bitches", viewPager.toString());
Log.d("MyDebugBitches", pagerAdapter.toString());
//here we give the pager its adapter
viewPager.setAdapter(pagerAdapter);//<-- this is causing exception
}
以下是此活动正在使用的布局xml R.layout.activity_data_set_view
。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="@+id/left_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="#111"/>
</android.support.v4.widget.DrawerLayout>
这是logcat:
03-11 12:31:54.476: E/AndroidRuntime(2893): FATAL EXCEPTION: main
03-11 12:31:54.476: E/AndroidRuntime(2893): Process: com.sweidenkopf.discretesuite, PID: 2893
03-11 12:31:54.476: E/AndroidRuntime(2893): java.lang.NullPointerException
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-11 12:31:54.476: E/AndroidRuntime(2893): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-11 12:31:54.476: E/AndroidRuntime(2893): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.Choreographer.doFrame(Choreographer.java:544)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.os.Handler.handleCallback(Handler.java:733)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.os.Handler.dispatchMessage(Handler.java:95)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.os.Looper.loop(Looper.java:136)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-11 12:31:54.476: E/AndroidRuntime(2893): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 12:31:54.476: E/AndroidRuntime(2893): at java.lang.reflect.Method.invoke(Method.java:515)
03-11 12:31:54.476: E/AndroidRuntime(2893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-11 12:31:54.476: E/AndroidRuntime(2893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-11 12:31:54.476: E/AndroidRuntime(2893): at dalvik.system.NativeStart.main(Native Method)
编辑:
我将null
检查更改为:
//initialize the view pager!
viewPager = (ViewPager) findViewById(R.id.pager);
if(viewPager == null){
Log.d("My Debug Bitches", "viewPager is null");
}
if(pagerAdapter == null){
Log.d("MyDebugBitches", "pagerAdapter is null");
}
这两个Log
来电都没有被触发,这让我觉得viewPager
和pagerAdapter
都不是null
这是我的寻呼机适配器:
private class DataSetViewPagerAdapter extends FragmentPagerAdapter{
public DataSetViewPagerAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
/**
* This method must return the number of tabs, which is the length of the
* view_pager_tabs string array (it contains the names of all of the tabs)
*/
@Override
public int getCount() {
return getResources().getStringArray(R.array.view_pager_tabs).length;
}
/**
* returns the element with in the view_pager_tabs array that corresponds with
* the given position
*/
@Override
public CharSequence getPageTitle(int position) {
return getResources().getStringArray(R.array.view_pager_tabs)[position];
}
}
答案 0 :(得分:2)
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
你的问题就在这里。您的getItem
无法返回null。在ViewPager
内部,getItem
为您在{{1}}中返回的片段创建了一些事务,并且您无法在事务中添加空片段