嗨,我知道有一些问题已经被问过,但我想知道为什么我的片段相互重叠。我遵循了以编程方式添加片段的所有解决方案并使用容器替换它,但我的片段仍然重叠...但是我在标签之间来回切换几次它完美地工作,因为片段不再重叠......任何人都可以帮我这个吗?
谢谢
这就是我以编程方式添加Fragment的方法:
getSupportFragmentManager().beginTransaction()
.add(R.id.leaderboard_layout, SingleplayerTab.newInstance(scores)).commit();
其中leaderboard_layout是FrameLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/leaderboard_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LeaderboardUI" >
</FrameLayout>
这是我要替换的代码:
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
FragmentTransaction fst = getSupportFragmentManager().beginTransaction();
fst.replace(R.id.leaderboard_layout, currentFragment);
fst.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fst.addToBackStack(null);
fst.commit();
}
**编辑**
这是我的代码,尝试删除并添加
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// begin a fragment transaction and replace the current transaction
FragmentTransaction fst = getSupportFragmentManager().beginTransaction();
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.leaderboard_layout);
fst.remove(currentFragment);
//fst.replace(R.id.leaderboard_layout, currentFragment);
//fst.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fst.add(R.id.leaderboard_layout, currentFragment);
fst.addToBackStack(null);
fst.commit();
}
05-26 09:35:12.688: E/AndroidRuntime(3108): FATAL EXCEPTION: main
05-26 09:35:12.688: E/AndroidRuntime(3108): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zootypers/com.example.zootypers.ui.LeaderboardUI}: java.lang.NullPointerException
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.os.Looper.loop(Looper.java:137)
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-26 09:35:12.688: E/AndroidRuntime(3108): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 09:35:12.688: E/AndroidRuntime(3108): at java.lang.reflect.Method.invoke(Method.java:511)
05-26 09:35:12.688: E/AndroidRuntime(3108): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-26 09:35:12.688: E/AndroidRuntime(3108): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-26 09:35:12.688: E/AndroidRuntime(3108): at dalvik.system.NativeStart.main(Native Method)
05-26 09:35:12.688: E/AndroidRuntime(3108): Caused by: java.lang.NullPointerException
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:384)
05-26 09:35:12.688: E/AndroidRuntime(3108): at com.example.zootypers.ui.LeaderboardUI$LBTabListener.onTabSelected(LeaderboardUI.java:88)
05-26 09:35:12.688: E/AndroidRuntime(3108): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
05-26 09:35:12.688: E/AndroidRuntime(3108): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:499)
05-26 09:35:12.688: E/AndroidRuntime(3108): at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:485)
05-26 09:35:12.688: E/AndroidRuntime(3108): at com.example.zootypers.ui.LeaderboardUI.onCreate(LeaderboardUI.java:42)
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.app.Activity.performCreate(Activity.java:5104)
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-26 09:35:12.688: E/AndroidRuntime(3108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)