我对fragments比较陌生,我有一个活动,其中包含一些使用fragments和viewpager的选项卡。
我的问题是,每当我改变方向或进入主屏幕时,我的应用程序就会崩溃。
以下是我的活动:

public class MainActivity extends SherlockFragmentActivity {

    ActionBar actionBar;
    ViewPager mPager;

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

        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mPager = (ViewPager) findViewById(R.id.pager);

        FragmentManager fm = getSupportFragmentManager();

        ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                actionBar.setSelectedNavigationItem(position);
            }
        };

        mPager.setOnPageChangeListener(pageChangeListener);

        MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);

        mPager.setAdapter(fragmentPagerAdapter);

        actionBar.setDisplayShowTitleEnabled(true);

        ActionBar.TabListener tabListener = new ActionBar.TabListener() {

            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            }

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                mPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
            }
        };

        Tab firstTab = mActionBar.newTab().setText("First Tab").setTabListener(tabListener);

        actionBar.addTab(firstTab);

        Tab secondTab = mActionBar.newTab()
            .setText("Second Tab")
            .setTabListener(tabListener);

        actionBar.addTab(secondTab);
    }
}

…下面是我的一个片段:
public class FirstFragment extends SherlockFragment implements ActionBar.TabListener{


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragmentone, container, false);

        // Stuff...

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }
}

…我的片段适配器:
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    final int PAGE_COUNT = 2;

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

    @Override
    public Fragment getItem(int arg0) {
        Bundle data = new Bundle();
        switch(arg0){
            case 0:
                FirstFragment firstFragment = new FirstFragment();
                data.putInt("current_page", arg0+1);
                firstFragment.setArguments(data);
                return firstFragment;

        case 1:
                SecondFragment secondFragment = new SecondFragment();
                data.putInt("current_page", arg0+1);
                secondFragment.setArguments(data);
                return secondFragment;
        }
        return null;
    }

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

…这是我所有的活动xml文件:
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

</RelativeLayout>

请帮忙,这让我头疼。
如果有帮助的话,我正在使用ActionBarSherlock库。
编辑:这是我的日志当它崩溃的时候
11-27 22:44:14.590: D/dalvikvm(803): newInstance failed: no <init>()
11-27 22:44:14.599: D/AndroidRuntime(803): Shutting down VM
11-27 22:44:14.599: W/dalvikvm(803): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
11-27 22:44:14.669: W/Trace(700): Unexpected value from nativeGetEnabledTags: 0
11-27 22:44:14.760: E/AndroidRuntime(803): FATAL EXCEPTION: main
11-27 22:44:14.760: E/AndroidRuntime(803): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pk.chemhelp/com.pk.chemhelp.Debug}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.pk.chemhelp.Debug$FirstFragment: make sure class name exists, is public, and has an empty constructor that is public
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.app.ActivityThread.access$700(ActivityThread.java:141)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.os.Looper.loop(Looper.java:137)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.app.ActivityThread.main(ActivityThread.java:5039)
11-27 22:44:14.760: E/AndroidRuntime(803):  at java.lang.reflect.Method.invokeNative(Native Method)
11-27 22:44:14.760: E/AndroidRuntime(803):  at java.lang.reflect.Method.invoke(Method.java:511)
11-27 22:44:14.760: E/AndroidRuntime(803):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-27 22:44:14.760: E/AndroidRuntime(803):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-27 22:44:14.760: E/AndroidRuntime(803):  at dalvik.system.NativeStart.main(Native Method)
11-27 22:44:14.760: E/AndroidRuntime(803): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.pk.chemhelp.Debug$FirstFragment: make sure class name exists, is public, and has an empty constructor that is public
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.support.v4.app.FragmentState.instantiate(Fragment.java:96)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1726)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:198)
11-27 22:44:14.760: E/AndroidRuntime(803):  at com.pk.chemhelp.Debug.onCreate(Debug.java:63)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.app.Activity.performCreate(Activity.java:5104)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-27 22:44:14.760: E/AndroidRuntime(803):  ... 12 more
11-27 22:44:14.760: E/AndroidRuntime(803): Caused by: java.lang.InstantiationException: can't instantiate class com.pk.chemhelp.Debug$FirstFragment; no empty constructor
11-27 22:44:14.760: E/AndroidRuntime(803):  at java.lang.Class.newInstanceImpl(Native Method)
11-27 22:44:14.760: E/AndroidRuntime(803):  at java.lang.Class.newInstance(Class.java:1319)
11-27 22:44:14.760: E/AndroidRuntime(803):  at android.support.v4.app.Fragment.instantiate(Fragment.java:384)
11-27 22:44:14.760: E/AndroidRuntime(803):  ... 19 more
11-27 22:44:14.880: W/ActivityManager(290):   Force finishing activity com.pk.chemhelp/.Debug
11-27 22:44:14.950: W/Trace(519): Unexpected value from nativeGetEnabledTags: 0

最佳答案

异常很明显,android需要完全访问您的类,以便在需要时(比如在配置更改时)实例化它。问题是,您将Fragment声明为FirstFragment类中的内部类,android系统无法实例化Debug,因为内部类总是绑定到封闭类的实例(在您的示例中为FirstFragment),因此Debug不能只是简单地实例化。
解决方案是将FirstFragment类与FirstFragment类分开,或者将Debug放在自己的Java文件中,或者将其保存在FirstFragment类中并使其成为cc>(因此它不再绑定到Debug类的实例):

public static class FirstFragment extends SherlockFragment implements ActionBar.TabListener{

10-05 18:01