Android Fragment& ViewPager:方向更改时崩溃

时间:2012-11-26 05:10:00

标签: android android-fragments actionbarsherlock android-viewpager android-orientation

我对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库。

编辑:这是我崩溃时遇到的logcat

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

1 个答案:

答案 0 :(得分:2)

异常非常明显,Android需要完全访问您的Fragment类,以便它可以在需要时实例化(例如在配置更改时)。问题是您在FirstFragment类中将Debug声明为内部类,并且Android系统无法实例化FirstFragment,因为内部类始终与实例关联封闭类(Debug在你的情况下),因此FirstFragment不能简单地实例化。

解决方案是将FirstFragment类与Debug类“分开”,方法是将FirstFragment放在自己的java文件中,或者将其保存在Debug中类和static(因此它不再绑定到Debug类的实例):

public static class FirstFragment extends SherlockFragment implements ActionBar.TabListener{