Reputation: 39
I have a very simple app with just one activity and a FrameLayout
in it. I have two layout A and B and I am loading them as fragments one at a time thus replacing the current fragment. The problem is that when I press the back button the app crashes. The fragment change is triggered by a button in the first fragment
MainActivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
else {
// Create a new Fragment to be placed in the activity layout
SelectLogInFragment firstFragment = new SelectLogInFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
}
}
}
public void login_email(View view)
{
// Create fragment and give it an argument specifying the article it should show
Log_In_form newFragment = new Log_In_form();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
SelectLogInFragment.java
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SelectLogInFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.select_log_in, container, false);
}
}
LogInForm is very similar to SelectLogInFragment
select_log_in.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.badass.david.mynewapplication.Top_Log"
android:id="@+id/top_log"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp" />
<fragment android:name="com.badass.david.mynewapplication.Bottom_Log"
android:id="@+id/bottom_log"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
Top_Log.java
public class Top_Log extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.top_log, container, false);
}
}
Bottom_Log is very similar
top_log.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/white" >
<TextView
android:id="@+id/logo_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Put Logo here" />
</LinearLayout>
Errors
06-20 12:00:06.773 20327-20327/com.badass.david.mynewapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.badass.david.mynewapplication, PID: 20327 android.view.InflateException: Binary XML file line #6: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770) at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) at android.view.LayoutInflater.inflate(LayoutInflater.java:511) at android.view.LayoutInflater.inflate(LayoutInflater.java:415) at com.badass.david.mynewapplication.SelectLogInFragment.onCreateView(SelectLogInFragment.java:16) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:979) at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1670) at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:586) at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:188) at android.app.Activity.onKeyUp(Activity.java:2576) at android.view.KeyEvent.dispatch(KeyEvent.java:3171) at android.app.Activity.dispatchKeyEvent(Activity.java:2831) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2438) at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4582) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4537) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4201) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4095) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4258) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4095) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4234) at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4421) at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2480) at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2074) at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2065) at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2457) at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:143) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) Caused by: java.lang.IllegalArgumentException: Binary XML file line
6: Duplicate id 0x7f0c0089, tag null, or parent id 0xffffffff with another fragment for com.badass.david.mynewapplication.Top_Log
at
android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293) at android.support.v4.view.LayoutInflaterCompatHC$Factory
To me it seems like it is trying to create a fragment that already exists so there is an ID conflict.. Is it the problem? How can I solve it?
Upvotes: 0
Views: 1568
Reputation: 51
you should replace file select_log_in.xml by:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/top_log"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp" />
<FrameLayout
android:id="@+id/bottom_log"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
and in the onCreateView method of SelectLogInFragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.select_log_in,null);
}
and in the onViewCreated method of SelectLogInFragment:
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewGroup topLogView = findViewById(R.id.top_log);
ViewGroup bottomLogView = findViewById(R.id.bottom_log);
if(topLogView != null){
topLogView.removeAllViews();
}
if(bottomLogView != null){
bottomLogView.removeAllViews();
}
getChildFragmentManager().beginTransaction().add(R.id.top_log,TopLogFragment,"top_log").commit();
getChildFragmentManager().beginTransaction().add(R.id.bottom_log,BottomLogFragment,"bottom_log").commit();
}
Upvotes: 0