简体   繁体   English

回到堆栈中的上一个片段时,应用程序崩溃

[英]App crashes when coming back to previous fragment in stack

I have a very simple app with just one activity and a FrameLayout in it. 我有一个非常简单的应用程序,其中只有一个活动和一个FrameLayout I have two layout A and B and I am loading them as fragments one at a time thus replacing the current fragment. 我有两个布局A和B,我一次将它们作为片段加载,从而替换了当前片段。 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 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 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 LogInForm与SelectLogInFragment非常相似

select_log_in.xml 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 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 Bottom_Log非常相似

top_log.xml 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.popBackStackS 06-20 12:00:06.773 20327-20327 / com.badass.david.mynewapplication E / AndroidRuntime:致命例外:主进程:com.badass.david.mynewapplication,PID:20327 android.view.InflateException:二进制XML文件行#6:错误放大android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)处的类片段,android.view.LayoutInflater.rInflate(LayoutInflater.java:813)处android.view.LayoutInflater.inflate(LayoutInflater.java: 511),位于com.badass.david.mynewapplication.SelectLogInFragment.onCreateView(SelectLogInFragment.java:16)的android.view.LayoutInflater.inflate(LayoutInflater.java:415),位于android.support.v4.app.Fragment.performCreateView(Fragment .java:1974),位于android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067),位于android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252),位于android.support.v4。 app.BackStackRecord.popFromBackStack(BackStackRecord.java:979)在android.support.v4.app.FragmentManagerImpl.popBackStackS tate(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(Vi tate(FragmentManager.java:1670)在android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:586)在android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:188)在android.app .com.android.internal.policy上的.Activity.onKeyUp(Activity.java:2576)在android.view.KeyEvent.dispatch(KeyEvent.java:3171)在android.app.Activity.dispatchKeyEvent(Activity.java:2831)在com.android.internal.policy。在android.view.ViewRootImpl $ ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4582)处运行impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:2438)在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4537) .view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4068)位于android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4121)位于android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4087)位于android.view.ViewRootImpl $ InputStage.apply(Vi上的android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4201) ewRootImpl.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. ewRootImpl.java:4095),位于android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:4258),位于android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4068),位于android.view.ViewRootImpl $ InputStage.onDeliverToNext (ViewRootImpl.java:4121)在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4087)在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4095)在android.view.ViewRootImpl $ InputStage。在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4121)在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4087)处交付(ViewRootImpl.java:4068)在android.view.ViewRootImpl $ AsyncInputStage android.view的.forward(ViewRootImpl.java:4234)android.view.ViewRootImpl $ ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4421)android.view.inputmethod.InputMethodManager $ PendingEvent.run(InputMethodManager.java:2480)在android.view。 android.view的inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2074)。 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 android.view.inputmethod.Inputmethod.InputMethodManager $ ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2457)at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)上的inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2065) .MessageQueue.nativePollOnce(本机方法)android.os.MessageQueue.next(MessageQueue.java:143)android.os.Looper.loop(Looper.java:130)android.app.ActivityThread.main(ActivityThread.java :5951)at java.lang.reflect.Method.invoke(本地方法)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)中的.java:1388)原因:java.lang.IllegalArgumentException:二进制XML文件行

6: Duplicate id 0x7f0c0089, tag null, or parent id 0xffffffff with another fragment for com.badass.david.mynewapplication.Top_Log 6:重复的ID 0x7f0c0089,标记为null或父ID 0xffffffff(带有com.badass.david.mynewapplication.Top_Log的另一个片段)

  at 

android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293) at android.support.v4.view.LayoutInflaterCompatHC$Factory android.support.v4.view.LayoutInflaterCompatHC $ Factory上的android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293)

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? 在我看来,它似乎正在尝试创建一个已经存在的片段,因此存在ID冲突。这是问题吗? How can I solve it? 我该如何解决?

you should replace file select_log_in.xml by: 您应该将文件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">

<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: 并在SelectLogInFragment的onCreateView方法中:

  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: 并在SelectLogInFragment的onViewCreated方法中:

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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM