3

我是相當新的碎片,我有一個活動,其中包含幾個選項卡使用片段和viewpager。Android碎片&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

也發佈LogCat錯誤。 – Sam

+0

你可以發佈錯誤的堆棧跟蹤嗎? – antew

+1

明天下午我會發布日誌。當我發佈這個消息時,我立即下了電腦。 *憤怒退出* – Pkmmte

回答

2

唯一的例外是很清楚的,Android的需要你Fragment的門類齊全的訪問,以便需要時可以對其進行實例化(像配置更改)。問題在於,你將FirstFragment聲明爲Debug類中的內部類,並且Android系統無法實例化FirstFragment,因爲內部類總是綁定到包含類的實例(在您的情況下爲Debug),所以FirstFragment不能簡單地實例化。

的解決方案是「獨立」的FirstFragmentDebug類類由要麼投入了自己的Java文件FirstFragment或保持它在Debug類,並使其static(所以它不再被綁定到Debug類的實例):

public static class FirstFragment extends SherlockFragment implements ActionBar.TabListener{ 
+0

謝謝,它的工作! – Pkmmte