Android空指针片段适配器

Android空指针片段适配器,android,android-fragments,nullpointerexception,Android,Android Fragments,Nullpointerexception,我有个问题。我的应用程序有2个活动。 1) 登录活动: public class Login extends Activity { Button login; EditText user,pw; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login);

我有个问题。我的应用程序有2个活动。 1) 登录活动:

public class Login extends Activity {

Button login;
EditText user,pw;

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

    login=(Button)findViewById(R.id.button);
    user=(EditText)findViewById(R.id.editText);
    pw=(EditText)findViewById(R.id.editText2);


    login.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
            // Switching to Register screen
            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(i);
        }

});




}
}
public class MainActivity extends FragmentActivity {


private static final int NUMBER_OF_PAGES = 3;



SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

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


    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

}

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



/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {
    private Fragment[] fragments = new Fragment[] { new Fragment1(), new Fragment2(), new Fragment3()};
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        return fragments[position];
    }

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


    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}


}
2) 主要活动:

public class Login extends Activity {

Button login;
EditText user,pw;

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

    login=(Button)findViewById(R.id.button);
    user=(EditText)findViewById(R.id.editText);
    pw=(EditText)findViewById(R.id.editText2);


    login.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
            // Switching to Register screen
            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(i);
        }

});




}
}
public class MainActivity extends FragmentActivity {


private static final int NUMBER_OF_PAGES = 3;



SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

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


    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

}

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



/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {
    private Fragment[] fragments = new Fragment[] { new Fragment1(), new Fragment2(), new Fragment3()};
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        return fragments[position];
    }

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


    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}


}
当我在点击按钮后执行程序时,我的应用程序将崩溃,文件为logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appfragment/com.example.appfragment.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    at android.app.ActivityThread.access$600(ActivityThread.java:130)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.example.appfragment.MainActivity.onCreate(MainActivity.java:39)
    at android.app.Activity.performCreate(Activity.java:5008)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    ... 11 more
问题出在该代码的主要活动上:

mViewPager.setAdapter(mSectionsPagerAdapter);

有什么问题吗?

让activity\u main使用id为R.id.pager的ViewPager?可能没有。。。也许我发现了问题。。。创建新活动后,我的旧xml被删除。。。我得重写它问题解决了谢谢