0

Ich habe eine sehr einfache App mit nur einer Aktivität und einem FrameLayout drin. Ich habe zwei Layouts A und B, und ich lade sie als Fragmente nacheinander, wodurch das aktuelle Fragment ersetzt wird. Das Problem ist, dass wenn ich den Zurück-Knopf drücke, die App abstürzt. Das Fragment Änderung wird durch einen Knopf im ersten Fragment ausgelöstApp stürzt ab, wenn ich zum vorherigen Fragment im Stapel zurückkomme

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 ist sehr ähnlich

select_log_in.xml

SelectLogInFragment
<?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 ist sehr ähnlich

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 20.327 bis 20.327/com.badass.david.myneapplication E/AndroidRuntime: FATALE AUSNAHME: Haupt Prozess: com.badass.david.mynewapplication, PID: 20327 android.view.InflateException: Binary XML-Datei Zeile # 6: Fehler aufblähenden Klasse Fragment bei android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:770) bei android.view.LayoutInflater.rInflate (LayoutInflater.java:813) bei android.view.LayoutInflater.inflate (LayoutInflater.java:511) bei android.view.LayoutInflater.inflate (LayoutInflater.java:415) bei com.badass.david.mynewapplication.SelectLogInFragment.onCreateView (SelectLogInFragment.java:16) bei android.support.v4.app.Fragment.performCreateView (Fragment.java:1974) bei android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1067) bei android.support.v4 .app.FragmentManagerImpl.moveToState (FragmentManager.java:1252) bei android.support.v4.app.BackStackRecord.popFromBackStack (BackStackRecord.java:979) bei android.support.v4.app.FragmentManagerImpl.popBackStackState (FragmentManager .java: 1670) unter android.support.v4.app.FragmentManagerImpl.popBackStackImmediate (FragmentManager.java:5 86) bei android.support.v4.app.FragmentActivity.onBackPressed (FragmentActivity.java:188) bei android.app.Activity.onKeyUp (Activity.java:2576) bei android.view.KeyEvent.dispatch (KeyEvent .java: 3171) bei android.app.Activity.dispatchKeyEvent (Activity.java:2831) unter com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent (PhoneWindow.java:2438) bei android.view.ViewRootImpl $ ViewPostImeInputStage.processKeyEvent (ViewRootImpl.java:4582) bei android.view.ViewRootImpl $ ViewPostImeInputStage. onProcess (ViewRootImpl.java:4537) bei android.view.ViewRootImpl $ InputStage.deliver (ViewRootImpl.java:4068) bei android.view.ViewRootImpl $ InputStage.onDeliverToNext (ViewRootImpl.java:4121) bei android .view.ViewRootImpl $ InputStage.forward (ViewRootImpl.java:4087) um android.view.ViewRootImpl $ AsyncInputStage.forward (ViewRootImpl.java:4201) bei android.view.ViewRootImpl $ InputStage.apply (ViewRootImpl.java:4095) bei android.view.ViewRootImpl $ AsyncInputStage.apply (ViewRootImpl. java: 4258) bei android.view.ViewRootImpl $ InputStage.deliver (ViewRootImpl.java:4068) bei android.view.ViewRootImpl $ InputStage.onDeliverToNext (ViewRootImpl.java:4121) bei android.view.ViewRootImpl $ InputStage.forward (ViewRootImpl.java:4087) bei android.view.ViewRootImpl $ InputStage.apply (ViewRootImpl.java:4095).210 bei android.view.ViewRootImpl $ InputStage.deliver (ViewRootImpl.java:4068) bei android.view.ViewRootImpl $ InputStage.onDeliverToNext (ViewRootImpl.java:4121) bei android.view.ViewRootImpl $ InputStage.forward (ViewRootImpl.java:4087) bei android.view.ViewRootImpl $ AsyncInputStage.forward (ViewRootImpl.java:4234) bei android.view.ViewRootImpl $ ImeInputStage.onFinishedInputEvent (ViewRootImpl.java:4421) bei android. view.inputmethod.InputMethodManager $ PendingEvent.run (InputMethodManager.java:2480) bei android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback (InputMethodManager.java:2074) bei android.view.inputmethod.InputMethodManager.finishedInputEvent (InputMethodManager.java:2065) bei android.view.inputmethod.InputMethodManager $ ImeInputEventSender .onInputEventFinished (InputMethodManager.java:2457) bei android.view.InputEventSender.dispatchInputEventFinished (InputEventSender.java:141) bei android.os.MessageQueue.nativePollOnce (native Methode) bei android.os.MessageQueue.next (Message .java: 143) bei android.os.Looper.loop (Looper.java:130) bei android.app.ActivityThread.main (ActivityThread.java:5951) bei java.lang.reflect.Method.invoke (native Methode) bei java. lang.reflect.Method.invoke (Methode.java:372) um com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1388) bei com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1183) verursacht durch: java.lang.IllegalArgumentException: Binary XML-Datei Zeile

6: Doppelte ID 0x7f0c0089, Tag null oder Eltern-ID 0xffffffff mit einem anderen Fragment für com.badass.david.mynewapplication.Top_Log

                    at 

android.support.v4.app.FragmentManagerImpl.onCreateView (FragmentManager.java:2293) bei android.support.v4.view.LayoutInflaterCompatHC $ Fabrik

Für mich scheint es, als ob es versucht, ein Fragment zu erstellen, das bereits existiert, so dass es einen ID-Konflikt gibt. Ist es das Problem? Wie kann ich es lösen?

+0

' com.badass.david 'Ihr Namespace heißt Badass, was Android-Studio wütend macht und es er wirft ror. – Vucko

Antwort

0

Sie sollten Datei select_log_in.xml durch ersetzen:

<?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> 

und in der onCreateView Methode von SelectLogInFragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.select_log_in,null); 
    } 

und in der onViewCreated Methode von 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(); 

}