[英]Android won't allow the transition of more than one fragment:
按照這里的教程和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()
但在新的replace(...)
命令之前不調用beginTransaction()
。
編輯:也許這不清楚。 我的意思是:
public void goToMain(View v) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, mainFragment);
transaction.addToBackStack(null);
transaction.commit();
}
等等。 您無法重復使用您的交易,因此請勿保存。 創建一個新的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.