我正在研究android Fragments。昨天我编写了与我现在正在做的相同的事情,但今天它崩溃了。我包含代码,请查看是否可以捕获其中的错误 主要FragmentActivity的代码
package com.example.fragmentswithswipetabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.viewPager);
viewpager.setAdapter(new CustomAdapter(getSupportFragmentManager()));
}
public class CustomAdapter extends FragmentPagerAdapter {
public CustomAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int i) {
// TODO Auto-generated method stub
Fragment fragment = null;
switch (i) {
case 0:
fragment = new FragmentA();
break;
case 1:
fragment = new FragmentB();
break;
case 3:
fragment = new FragmentC();
break;
}
return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
if (position == 0) {
return "Tab 1";
} else if (position == 1) {
return "Tab 2";
} else if(position == 2){
return "Tab 3";
}
return null;
}
}
}
activity_main.xml的代码
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/viewPagerStrip"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_gravity="top"
android:background="#DDDDDD"
android:gravity="center" >
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
这里只是一个片段的代码(只是为了你的怀疑)
package com.example.fragmentswithswipetabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1, container, false);
}
}
记录Cat信息
07-04 07:07:05.204: D/dalvikvm(150): GC_EXPLICIT freed 100K, 51% free 2975K/5959K, external 1289K/1546K, paused 96ms
07-04 07:07:05.394: D/AndroidRuntime(799): Shutting down VM
07-04 07:07:05.394: W/dalvikvm(799): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-04 07:07:05.404: E/AndroidRuntime(799): FATAL EXCEPTION: main
07-04 07:07:05.404: E/AndroidRuntime(799): java.lang.NullPointerException
07-04 07:07:05.404: E/AndroidRuntime(799): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
07-04 07:07:05.404: E/AndroidRuntime(799): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
07-04 07:07:05.404: E/AndroidRuntime(799): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
07-04 07:07:05.404: E/AndroidRuntime(799): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
07-04 07:07:05.404: E/AndroidRuntime(799): at android.support.v4.view.ViewPager.populate(ViewPager.java:1048)
07-04 07:07:05.404: E/AndroidRuntime(799): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
07-04 07:07:05.404: E/AndroidRuntime(799): at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
07-04 07:07:05.404: E/AndroidRuntime(799): at android.os.Handler.handleCallback(Handler.java:587)
07-04 07:07:05.404: E/AndroidRuntime(799): at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 07:07:05.404: E/AndroidRuntime(799): at android.os.Looper.loop(Looper.java:123)
07-04 07:07:05.404: E/AndroidRuntime(799): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-04 07:07:05.404: E/AndroidRuntime(799): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 07:07:05.404: E/AndroidRuntime(799): at java.lang.reflect.Method.invoke(Method.java:507)
07-04 07:07:05.404: E/AndroidRuntime(799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-04 07:07:05.404: E/AndroidRuntime(799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-04 07:07:05.404: E/AndroidRuntime(799): at dalvik.system.NativeStart.main(Native Method)
07-04 07:07:05.404: W/ActivityManager(61): Force finishing activity com.example.fragmentswithswipetabs/.MainActivity
07-04 07:07:05.924: W/ActivityManager(61): Activity pause timeout for HistoryRecord{40543390 com.example.fragmentswithswipetabs/.MainActivity}
请注意,我包含三个片段,每个片段都有一个简单的文本视图。 应用程序启动,当我滑动到其他片段时崩溃
答案 0 :(得分:2)
case 0:
fragment = new FragmentA();
break;
case 1:
fragment = new FragmentB();
break;
case 3:
fragment = new FragmentC();
break;
应该没有案例3,应该是2.:)
答案 1 :(得分:0)
因此我们需要在activityCreated()
之后添加以下代码 @Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}