在null对象引用上的java.lang.Class java.lang.Object.getClass()'

时间:2017-01-24 11:07:25

标签: android xml nullpointerexception android-viewpager

我尝试将viewPager与tablayout一起使用,但是当我使用视图寻呼机设置适配器时,我收到错误“java.lang.NullPointerException:尝试调用虚方法'java.lang.Class java.lang.Object.getClass ()对空对象引用“我不明白为什么会显示此错误。

查看寻呼机设置适配器:

viewPager= (ViewPager) findViewById(R.id.viewPager_id);
  viewPager.setAdapter(newViewPagerAdapterUseThisClass(getSupportFragmentManager(),14));

适配器clss:

class ViewPagerAdapterUseThisClass extends FragmentPagerAdapter {
    int countTabItem;
     ViewPagerAdapterUseThisClass(FragmentManager fm,int contTabItem) {
        super(fm);

        this.countTabItem=contTabItem;
    }

    @Override
    public Fragment getItem(int position) {

        if(position==0){
            EnthusamTab enthusamTab=new EnthusamTab();
            return enthusamTab;
        }

        return null;
    }

    @Override
    public int getCount() {
        return countTabItem;
    }
}

查看寻呼机xml文件:

 <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/tab_layout_id"
            android:id="@+id/viewPager_id"/>

错误日志:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                                                                                  at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:380)
                                                                                  at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:375)
                                                                                  at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:103)
                                                                                  at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
                                                                                  at android.support.v4.view.ViewPager.populate(ViewPager.java:1248)
                                                                                  at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
                                                                                  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                                                                                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                  at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
                                                                                  at android.view.View.measure(View.java:19861)
                                                                                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
                                                                                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
                                                                                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
                                                                                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                                                                                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6343)
                                                                                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                                  at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                                  at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6126)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

9 个答案:

答案 0 :(得分:42)

当您使用null getItem(int position)方法返回PagerAdapter时会发生此异常。

您必须在getCount()方法设置返回值(n),并在Fragment方法中为所有(0 .. n-1)位置返回getItem(int position)

例如,如果从getCount()方法返回3,则必须在Fragment方法中为位置0,1和2返回非空getItem(int position)。如果为位置0,1或2返回null,则会得到:

  

java.lang.NullPointerException:尝试调用虚方法   '对象上的'java.lang.Class java.lang.Object.getClass()'   参考

private static class MyPagerAdapter extends FragmentPagerAdapter {
    ...

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new XFragment();
            case 1:
                return new YFragment();
            case 2:
                return new ZFragment();
            default:
                return null; // Problem occurs at this condition!
        }
    }
}

答案 1 :(得分:10)

当您返回的片段少于指定的计数时,通常会出现此错误。例如,返回count = 3,并在第二个位置返回null。 请注意,count是一个基于零的索引。

答案 2 :(得分:3)

返回所有计数。试试这个:

Override
    public Fragment getItem(int position) {
            EnthusamTab enthusamTab=new EnthusamTab();
            return enthusamTab;

    }

答案 3 :(得分:2)

根据我看到你在这个方法中返回null:

@Override
    public Fragment getItem(int position) {

        if(position==0){
            EnthusamTab enthusamTab=new EnthusamTab();
            return enthusamTab;
        }

        return null;
    }

答案 4 :(得分:0)

public Fragment getItem(int position) {
        if (position == 0) {

            Tab1 tab1 = new Tab1();
            return tab1;
        }
        if (position == 2) {
            Tab2 tab2 = new Tab2();
            return tab2;
        } else {
            Tab3 tab3 = new Tab3();
            return tab3;
        }

    }

答案 5 :(得分:0)

尝试在getCount中对所有片段进行计数:例如1..5

@Override
public int getCount() {
    return 5; //her 
}

答案 6 :(得分:0)

仅当您尝试使用方法getCount()返回的值大于在选项卡中的初始值时,才会发生此类异常。 更改您的返回值。

答案 7 :(得分:0)

我收到此错误,是因为我忘记设置break命令。首先检查您的中断运算符。

现在我有了以下代码:

@Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new FollowersFragment();
            case 1:
                return new PostsFragment();
            case 2:
                return new ReactsFragment();
            case 3:
                return new StoriesFragment();
            case 4:
                return new FollowersFragment();
            case 5:
                return new FollowersFragment();
            case 6:
                return new FollowersFragment();
        }
        return null;
    }

答案 8 :(得分:0)

只需使用下面给出的代码。它避免了空错误并可以平稳运行

{{1}}