[英]Android won't allow the transition of more than one fragment:
Following the tutorial here and some help from stackoverflow I was able to make a button switch the replace the current fragment with another. 按照这里的教程和stackoverflow的一些帮助,我能够进行按钮切换,将当前片段替换为另一个。 To elaborate I can replace the current fragment but if I wish to replace the one after that with a button the app crashes.
为了详细说明,我可以替换当前片段,但如果我希望用一个按钮替换之后的应用程序崩溃。 My code:
我的代码:
MainActivity: 主要活动:
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;
}
}
}
Login fragment xml: 登录片段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>
Main fragment xml: 主片段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>
As you can see the code for the two different transactions is exactly the same... Why will android not allow the switching of fragments more than once? 正如您所看到的,两个不同事务的代码完全相同...为什么android不允许多次切换片段?
EDIT: Also for whatever reason my logcat is empty 编辑:无论出于何种原因,我的logcat都是空的
You call commit()
on the transaction but you don't call beginTransaction()
before the new replace(...)
command. 您在事务上调用
commit()
但在新的replace(...)
命令之前不调用beginTransaction()
。
Edit: Maybe this was not clear. 编辑:也许这不清楚。 I meant:
我的意思是:
public void goToMain(View v) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, mainFragment);
transaction.addToBackStack(null);
transaction.commit();
}
and so on. 等等。 You can not reuse your transaction, so don't save it.
您无法重复使用您的交易,因此请勿保存。 Create a new one.
创建一个新的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.