按照教程 here在 stackoverflow 的一些帮助下,我能够使用按钮切换将当前 fragment 替换为另一个 fragment 。详细来说,我可以替换当前的 fragment ,但如果我想用按钮替换之后的 fragment ,应用程序就会崩溃。我的代码:
主要 Activity :
public class MainActivity extends Activity {
public FragmentTransaction transaction = getFragmentManager().beginTransaction();
public Fragment loginFragment = new LoginFragment();
public Fragment mainFragment = new MainFragment();
public Fragment settingsFragment = new SettingsFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, loginFragment).commit();
} else {
getFragmentManager().beginTransaction().add(R.id.container, mainFragment).commit();
}
}
public void goToMain(View v) {
transaction.replace(R.id.container, mainFragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void goToSettings(View v) {
transaction.replace(R.id.container, settingsFragment);
transaction.addToBackStack(null);
transaction.commit();
}
public static class LoginFragment extends Fragment {
public LoginFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
return rootView;
}
}
public static class MainFragment extends Fragment {
public MainFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
public static class SettingsFragment extends Fragment {
public SettingsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_settings, container, false);
return rootView;
}
}
}
登录 fragment xml:
<RelativeLayout 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"
tools:context="com.pointlight.kingdomcraft.MainActivity$LoginFragment" >
<Button
android:id="@+id/button_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="goToMain"
android:text="@string/action_submit" />
</RelativeLayout>
主要 fragment xml:
<RelativeLayout 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"
tools:context="com.pointlight.kingdomcraft.MainActivity$MainFragment" >
<Button
android:id="@+id/button_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="goToSettings"
android:text="@string/action_settings" />
</RelativeLayout>
正如你所看到的,两个不同事务的代码是完全相同的......为什么android不允许 fragment 切换多次?
编辑:无论出于何种原因,我的 logcat 都是空的
最佳答案
您对事务调用了 commit()
,但在新的 replace(...)
之前没有调用 beginTransaction()
命令。
编辑:也许这还不清楚。我的意思是:
public void goToMain(View v) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, mainFragment);
transaction.addToBackStack(null);
transaction.commit();
}
等等。您无法重复使用您的交易,因此请勿保存它。创建一个新的。
关于java - Android不允许过渡多个fragment :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23205772/