当我写这段代码时
viewPager.setCurrentItem(tab.getPosition());
在ontabselected方法中,我的应用程序崩溃了。总的来说,我的申请工作正常。这是我的完整代码
package com.hamza.aurasalon;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar.Tab;
import android.support.v4.app.FragmentTransaction;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class AboutUs extends FragmentActivity implements TabListener {
private ActionBar actionBar;
private ViewPager viewPager;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
intialization();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void intialization() {
// TODO Auto-generated method stub
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("MUMTAZ ANWAR");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("CREDIENTIALS");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
viewPager = (ViewPager) findViewById(R.id.pagger);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onTabReselected(android.app.ActionBar.Tab tab,
android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(android.app.ActionBar.Tab tab,
android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
// viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
类MyAdapter扩展了FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragment = null;
if (arg0 == 0) {
fragment = new BlankFragment();
}
if (arg0 == 1) {
fragment = new BlankFragmenta();
}
return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
}
logcat错误
07-09 15:52:09.837: W/dalvikvm(13392): threadid=1: thread exiting with uncaught exception (group=0x415ba9a8)
07-09 15:52:09.850: E/AndroidRuntime(13392): FATAL EXCEPTION: main
07-09 15:52:09.850: E/AndroidRuntime(13392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hamza.aurasalon/com.hamza.aurasalon.AboutUs}: java.lang.NullPointerException
07-09 15:52:09.850: E/AndroidRuntime(13392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
07-09 15:52:09.850: E/AndroidRuntime(13392): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
07-09 15:52:09.850: E/AndroidRuntime(13392): at android.app.ActivityThread.access$600(ActivityThread.java:162)
07-09 15:52:09.850: E/AndroidRuntime(13392): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
07-09 15:52:09.850: E/AndroidRuntime(13392): at android.os.Handler.dispatchMessage(Handler.java:107)
07-09 15:52:09.850: E/AndroidRuntime(13392): at android.os.Looper.loop(Looper.java:194)
07-09 15:52:09.850: E/AndroidRuntime(13392): at android.app.ActivityThread.main(ActivityThread.java:5371)
07-09 15:52:09.850: E/AndroidRuntime(13392): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 15:52:09.850: E/AndroidRuntime(13392): at java.lang.reflect.Method.invoke(Method.java:525)
07-09 15:52:09.850: E/AndroidRuntime(13392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-09 15:52:09.850: E/AndroidRuntime(13392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-09 15:52:09.850: E/AndroidRuntime(13392): at dalvik.system.NativeStart.main(Native Method)
07-09 15:52:09.850: E/AndroidRuntime(13392): Caused by: java.lang.NullPointerException
07-09 15:52:09.850: E/AndroidRuntime(13392): at com.hamza.aurasalon.AboutUs.onTabSelected(AboutUs.java:85)
07-09 15:52:09.850: E/AndroidRuntime(13392): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
07-09 15:52:09.850: E/AndroidRuntime(13392): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:499)
07-09 15:52:09.850: E/AndroidRuntime(13392): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:485)
07-09 15:52:09.850: E/AndroidRuntime(13392): at com.hamza.aurasalon.AboutUs.intialization(AboutUs.java:47)
07-09 15:52:09.850: E/AndroidRuntime(13392): at com.hamza.aurasalon.AboutUs.onCreate(AboutUs.java:28)
07-09 15:52:09.850: E/AndroidRuntime(13392): at android.app.Activity.performCreate(Activity.java:5122)
07-09 15:52:09.850: E/AndroidRuntime(13392): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
07-09 15:52:09.850: E/AndroidRuntime(13392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
07-09 15:52:09.850: E/AndroidRuntime(13392): ... 11 more