可滑动查看的Null异常错误-Android Studio

时间:2018-11-11 16:01:28

标签: java android android-studio view android-tablayout

尝试运行应用程序并在视图之间滑动时,应用程序崩溃,并出现空对象引用错误。我仔细检查了我的getItem方法,以确保它可以处理此问题,但该应用程序仍然崩溃。该应用程序可以很好地运行第一个视图,但是在尝试滑动到第二个和第三个视图时会崩溃。

如果有人能澄清错误所在,将不胜感激。

我还在代码下方包含了错误日志。

public class MainActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        //returning the current tabs
        switch (position){
            case 0:
                Tab1 tab1 = new Tab1();
                return tab1;
            case 1:
                Tab2 tab2 = new Tab2();
                return tab2;
            case 3:
                Tab3 tab3 = new Tab3();
                return tab3;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }
}
}

这是错误日志;

  

java.lang.NullPointerException:尝试调用虚拟方法   空对象上的'java.lang.Class java.lang.Object.getClass()'   参考                                                                          在   android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)                                                                          在   android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)                                                                          在   android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:104)                                                                          在android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)                                                                          在android.support.v4.view.ViewPager.populate(ViewPager.java:1216)                                                                          在android.support.v4.view.ViewPager.populate(ViewPager.java:1084)                                                                          在android.support.v4.view.ViewPager $ 3.run(ViewPager.java:267)                                                                          在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:911)                                                                          在android.view.Choreographer.doCallbacks(Choreographer.java:723)                                                                          在android.view.Choreographer.doFrame(Choreographer.java:655)                                                                          在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:897)                                                                          在android.os.Handler.handleCallback(Handler.java:790)                                                                          在android.os.Handler.dispatchMessage(Handler.java:99)                                                                          在android.os.Looper.loop(Looper.java:164)                                                                          在android.app.ActivityThread.main(ActivityThread.java:6494)                                                                          在java.lang.reflect.Method.invoke(本机方法)                                                                          在   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

1 个答案:

答案 0 :(得分:1)

问题出在此代码中:

@Override
public Fragment getItem(int position) {
    //returning the current tabs
    switch (position){
        case 0:
            Tab1 tab1 = new Tab1();
            return tab1;
        case 1:
            Tab2 tab2 = new Tab2();
            return tab2;
        case 3:
            Tab3 tab3 = new Tab3();
            return tab3;
        default:
            return null;
    }
}

具体来说,此行:

case 3:

应改为case 2。现在,您将从null案例返回default进入第三页。


顺便说一句,我建议以后改变这种情况的处理方式。我认为您应该这样做:而不是从默认情况下返回null

case default:
    throw new IllegalArgumentException("unexpected position: " + position);

如果您有此位置而不是return null,那么错误将会更加明显。您已经看过

  

IllegalArgumentException:意外位置:2

case 3中可以帮助您实现错字。

通常,当程序位于不应该出现的位置时,应该始终抛出异常,而不是返回一些默认值。