有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ViewPager。setAdapter()引发NullPointException;viewPager和pagerAdapter似乎不为空?

因此,我在混淆视听地使用ViewPagerNavigation Drawer。我让Navigation Drawer工作

然而,试图调用viewPager.setAdapter(pagerAdapter)是在抛出一个NullPointerException

我正在检查viewPagerpagerAdapter是否为null,但当我尝试Log调用它们各自的toString()方法时,会发生以下情况:

03-11 12:31:54.156: D/My Debug Bitches(2893): 安卓.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{}如何返回一些奇怪的安卓.support.v4信息?我认为这可能与这个问题有关

下面是我的viewPagerpagerAdapter的初始化。我标记了导致异常的行

@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:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" >

<安卓.support.v4.view.ViewPager xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/pager"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" />

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

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        安卓:id="@+id/content_frame"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent" />

    <!-- 安卓: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
        安卓:id="@+id/left_drawer"
        安卓:layout_width="240dp"
        安卓:layout_height="match_parent"
        安卓:layout_gravity="start"
        安卓:choiceMode="singleChoice"
        安卓:divider="@安卓:color/transparent"
        安卓:dividerHeight="0dp"
        安卓:background="#111"/>
</安卓.support.v4.widget.DrawerLayout>

下面是日志:

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 安卓.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.support.v4.view.ViewPager.populate(ViewPager.java:982)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at com.安卓.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at com.安卓.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.Choreographer.doCallbacks(Choreographer.java:574)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.Choreographer.doFrame(Choreographer.java:544)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.os.Handler.handleCallback(Handler.java:733)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.os.Handler.dispatchMessage(Handler.java:95)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.os.Looper.loop(Looper.java:136)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-11 12:31:54.476: E/AndroidRuntime(2893):     at com.安卓.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调用都没有被触发,这让我觉得viewPagerpagerAdapter都不是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) 个答案