我的SoundFragment.java :( stackoverflow警告我,为什么?)
public class SoundFragment extends Fragment {
TabHost tabHost;
Activity activity;
public static SoundFragmentBGM bgmFragment;
public static SoundFragmentSoundEffect soundEffectFragment;
private static int count =1;
public static SoundFragment newInstance(String param1, String param2) {
SoundFragment fragment = new SoundFragment();
return fragment;
}
public SoundFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// View initialization
GCLog.d("Hideaki", "STARTING INFLATE VIEW OF SOUND ==" + count++);
View view = inflater.inflate(R.layout.fragment_sound, container, false);
initSoundStackManager(view);
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if (tabId.equals("BGMtab")) {
soundEffectFragment.onStopMedia();
tabHost.setCurrentTab(0);
} else {
bgmFragment.onStopMedia();
tabHost.setCurrentTab(1);
}
}
});
return view;
}
void initSoundStackManager(View view) {
tabHost = (TabHost) view.findViewById(R.id.soundtabhost);
tabHost.setup();
TabHost.TabSpec spec;
//create BGMtab
View viewBGM = LayoutInflater.from(activity).inflate(R.layout.tab_bgm_layout, null);
spec = tabHost.newTabSpec("BGMtab").setIndicator(viewBGM).setContent(R.id.tabBGM);
tabHost.addTab(spec);
//create 効果音tab
View viewSoundEffect = LayoutInflater.from(activity).inflate(R.layout.tab_kokaon_layout, null);
spec = tabHost.newTabSpec("効果音tab").setIndicator(viewSoundEffect).setContent(R.id.tabSoundEffect);
tabHost.addTab(spec);
FragmentManager fm = getActivity().getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (bgmFragment == null) {
bgmFragment = new SoundFragmentBGM();
GCLog.d("Hideaki", "INSTALL BGMFRAGMENT");
}
ft.replace(R.id.gc_sound_bgm_fragment, bgmFragment);
if (soundEffectFragment == null) {
soundEffectFragment = new SoundFragmentSoundEffect();
GCLog.d("Hideaki", "INSTALL soundEffectFragment");
}
ft.replace(R.id.gc_sound_effect_fragment, soundEffectFragment);
ft.addToBackStack(null);
//set default tab
if (SoundFragmentSoundEffect.se_mediaPlayer.isPlaying()) {
tabHost.setCurrentTab(1);
} else {
tabHost.setCurrentTab(0);
}
ft.commit();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
HomeActivity.soundLoaded = true;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
this.activity = activity;
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public void onDestroyView() {
super.onDestroyView();
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(soundEffectFragment);
ft.remove(bgmFragment);
ft.commit();
}
}
我的fragment_sound.xml,其中包含2个子片段。
> <LinearLayout
> xmlns:android="http://schemas.android.com/apk/res/android"
> xmlns:tools="http://schemas.android.com/tools"
> android:layout_width="match_parent"
> android:layout_height="match_parent"
> android:orientation="vertical"
> tools:context="jp.porthy.getclub.fragment.SoundFragment">
>
> <TabHost
> android:id="@+id/soundtabhost"
> android:layout_width="match_parent"
> android:layout_height="match_parent">
>
> <LinearLayout
> android:layout_width="match_parent"
> android:layout_height="match_parent"
> android:orientation="vertical">
>
> <TabWidget
> android:id="@android:id/tabs"
> android:layout_width="match_parent"
> android:showDividers="none"
> android:layout_height="38dp" />
>
> <FrameLayout
> android:id="@android:id/tabcontent"
> android:layout_width="match_parent"
> android:layout_height="match_parent">
>
> <LinearLayout
> android:id="@+id/tabBGM"
> android:layout_width="match_parent"
> android:layout_height="match_parent"
> android:orientation="vertical">
>
> <FrameLayout
> android:id="@+id/gc_sound_bgm_fragment"
> android:layout_width="fill_parent"
> android:layout_height="fill_parent">
>
> </FrameLayout>
> </LinearLayout>
>
> <LinearLayout
> android:id="@+id/tabSoundEffect"
> android:layout_width="match_parent"
> android:layout_height="match_parent"
> android:orientation="vertical">
>
> <FrameLayout
> android:layout_width="fill_parent"
> android:layout_height="fill_parent"
> android:id="@+id/gc_sound_effect_fragment">
>
> </FrameLayout>
> </LinearLayout>
> </FrameLayout>
> </LinearLayout>
> </TabHost>
>
> </LinearLayout>
我的logcat错误:
07-14 16:20:47.893 28471-28471/jp.porthy.getclub W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40aaf210) 07-14 16:20:47.903 28471-28471/jp.porthy.getclub E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalArgumentException: No view found for id 0x7f0c00d9 for fragment SoundFragmentBGM{420d43b8 #1 id=0x7f0c00d9} at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:822) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032) at android.app.BackStackRecord.run(BackStackRecord.java:622) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) at android.os.Handler.handleCallback(Handler.java:605) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4441) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) at dalvik.system.NativeStart.main(Native Method)
我尝试在加载视图后和延迟视图后添加延迟时间,但它可以解决我的问题。 我反复附加和分离SoundFragment时出错。
请帮我调试,非常感谢。