我的项目中有一个片段寻呼机,其中还包含一个操作栏sherlock。 当用户滑动屏幕时,页面将会更改,但我想在用户单击选项卡时执行此操作。当我运行应用程序时,会发生空指针异常。这是我的代码:
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
int position = tab.getPosition();
pager.setCurrentItem(position);
}
这是我的pagerAdapter类:
public class PagesAdapter extends FragmentPagerAdapter {
private Fragment fragment;
public PagesAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
fragment = new FragmentA();
break;
case 1:
fragment = new FragmentB();
break;
case 2:
fragment = new FragmentC();
break;
default:
fragment = null;
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
}
这是我的logCat:
01-06 18:00:00.806: E/Trace(2268): error opening trace file: No such file or directory (2)
01-06 18:00:02.134: D/AndroidRuntime(2268): Shutting down VM
01-06 18:00:02.146: W/dalvikvm(2268): threadid=1: thread exiting with uncaught exception (group=0xa6206908)
01-06 18:00:02.158: E/AndroidRuntime(2268): FATAL EXCEPTION: main
01-06 18:00:02.158: E/AndroidRuntime(2268): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.saoshyance.testactionbarsherlock/com.saoshyance.testactionbarsherlock.MainActivity}: java.lang.NullPointerException
01-06 18:00:02.158: E/AndroidRuntime(2268): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-06 18:00:02.158: E/AndroidRuntime(2268): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-06 18:00:02.158: E/AndroidRuntime(2268): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-06 18:00:02.158: E/AndroidRuntime(2268): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-06 18:00:02.158: E/AndroidRuntime(2268): at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 18:00:02.158: E/AndroidRuntime(2268): at android.os.Looper.loop(Looper.java:137)
01-06 18:00:02.158: E/AndroidRuntime(2268): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-06 18:00:02.158: E/AndroidRuntime(2268): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 18:00:02.158: E/AndroidRuntime(2268): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 18:00:02.158: E/AndroidRuntime(2268): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-06 18:00:02.158: E/AndroidRuntime(2268): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-06 18:00:02.158: E/AndroidRuntime(2268): at dalvik.system.NativeStart.main(Native Method)
01-06 18:00:02.158: E/AndroidRuntime(2268): Caused by: java.lang.NullPointerException
01-06 18:00:02.158: E/AndroidRuntime(2268): at com.saoshyance.testactionbarsherlock.MainActivity.onTabSelected(MainActivity.java:77)
01-06 18:00:02.158: E/AndroidRuntime(2268): at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabSelected(ActionBarWrapper.java:356)
01-06 18:00:02.158: E/AndroidRuntime(2268): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
01-06 18:00:02.158: E/AndroidRuntime(2268): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:499)
01-06 18:00:02.158: E/AndroidRuntime(2268): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:485)
01-06 18:00:02.158: E/AndroidRuntime(2268): at com.actionbarsherlock.internal.app.ActionBarWrapper.addTab(ActionBarWrapper.java:389)
01-06 18:00:02.158: E/AndroidRuntime(2268): at com.saoshyance.testactionbarsherlock.MainActivity.onCreate(MainActivity.java:45)
01-06 18:00:02.158: E/AndroidRuntime(2268): at android.app.Activity.performCreate(Activity.java:5104)
01-06 18:00:02.158: E/AndroidRuntime(2268): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-06 18:00:02.158: E/AndroidRuntime(2268): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-06 18:00:02.158: E/AndroidRuntime(2268): ... 11 more
位置1和2是正常的但是在位置0(起始片段)上它被撞坏了。