遵循教程here和stackoverflow的一些帮助后,我能够进行按钮切换,将当前片段替换为另一个片段。详细地说,我可以替换当前片段,但是如果我想用一个按钮替换之后的片段,应用程序将崩溃。我的代码:
主要活动:
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;
}
}
}
登录片段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>
主要片段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不允许多次切换片段?
编辑:也无论出于什么原因我的logcat为空
最佳答案
您在事务上调用commit()
,但是在新的beginTransaction()
命令之前没有调用replace(...)
。
编辑:也许这还不清楚。我的意思是:
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/