您正在开发一个加载fragmentActivity的应用程序。片段活动包含带有4个选项卡的tabhost,每个选项卡加载片段。当我运行应用程序时应用程序运行顺畅,但是当我们按主页按钮或任何其他应用程序运行时,我的应用程序崩溃... 。
这是我的日志猫
09-12 05:47:39.055: E/AndroidRuntime(3692): FATAL EXCEPTION: main
09-12 05:47:39.055: E/AndroidRuntime(3692): java.lang.RuntimeException: Parcel: unable to marshal value WelcomeFragment{40d3ae00}
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Parcel.writeValue(Parcel.java:1235)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Parcel.writeList(Parcel.java:622)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Parcel.writeValue(Parcel.java:1195)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Parcel.writeMapInternal(Parcel.java:591)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Parcel.writeMap(Parcel.java:575)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Parcel.writeValue(Parcel.java:1166)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Parcel.writeMapInternal(Parcel.java:591)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Bundle.writeToParcel(Bundle.java:1627)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Parcel.writeBundle(Parcel.java:605)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2252)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3052)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Handler.handleCallback(Handler.java:725)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.os.Looper.loop(Looper.java:137)
09-12 05:47:39.055: E/AndroidRuntime(3692): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-12 05:47:39.055: E/AndroidRuntime(3692): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 05:47:39.055: E/AndroidRuntime(3692): at java.lang.reflect.Method.invoke(Method.java:511)
09-12 05:47:39.055: E/AndroidRuntime(3692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-12 05:47:39.055: E/AndroidRuntime(3692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-12 05:47:39.055: E/AndroidRuntime(3692): at dalvik.system.NativeStart.main(Native Method)
这是我的tabActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
View absView=LayoutInflater.from(this).inflate(R.layout.abs_layout, null);
actionBar.setCustomView(absView);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
setContentView(R.layout.activity_tab_layout);
tabHost =(TabHost) findViewById(R.id.myTabHost);
tabHost.setup();
if (savedInstanceState != null) {
initializeTabHost();
tabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
backStacks=(HashMap<String, Stack<Fragment>>) savedInstanceState.getSerializable("stacks");
}
else
{
backStacks = new HashMap<String, Stack<Fragment>>();
for(int i=0;i<stackKey.length;i++)
backStacks.put(stackKey[i], new Stack<Fragment>());
initializeTabHost();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// super.onSaveInstanceState(outState);
outState.putString("tab", tabHost.getCurrentTabTag());
outState.putSerializable("stacks", backStacks);
}
private void initializeTabHost() {
tabHost.setOnTabChangedListener(this);
addTab(this, stackKey[0], labels[0],WelcomeFragment.class,R.drawable.menu_home);
addTab(this, stackKey[1], labels[1], ShopListFragment.class, R.drawable.menu_shops);
addTab(this, stackKey[2], labels[2], MyDealsFragment.class, R.drawable.menu_mydeals);
addTab(this, stackKey[3], labels[3], MyProfileFragment.class, R.drawable.menu_profile);
addTab(this, stackKey[4], labels[4], SettingFragment.class, R.drawable.menu_settings_on);
}
private void addTab(TabActivity activity,String tag,int label,Class<?> class1, int image)
{
Stack<Fragment> backStack = backStacks.get(tag);
View tabView=createTabView(label, image);
TabSpec tabSpec = tabHost.newTabSpec(tag).setIndicator(tabView).setContent(activity.new TabFactory(activity));
Fragment fragment = (Fragment) Fragment.instantiate(activity, class1.getName(),null);
if(backStack.isEmpty())
if(fragment!=null)
backStack.push(fragment);
tabHost.addTab(tabSpec);
}
这是我的WelcomeFragment
public class WelcomeFragment extends Fragment
{
ViewPageAdapter mAdapter;
ViewPager mPager;
CirclePageIndicator mIndicator;
List<Fragment> fragments;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_welcome_layout,null);
mPager=(ViewPager)view.findViewById(R.id.myViewPager);
mIndicator=(CirclePageIndicator)view.findViewById(R.id.circlePageIndicator1);
mAdapter = new ViewPageAdapter(getActivity());
mPager.setAdapter(mAdapter);
mIndicator.setViewPager(mPager);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}