我有一个非常简单的应用程序,只有一个活动,其中包含FrameLayout
。我有两个布局A和B,我将它们一次加载为一个片段,从而替换当前片段。问题是,当我按下后退按钮时,应用程序崩溃了。片段更改由第一个片段中的按钮
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与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非常相似
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>
错误
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) 在android.view.LayoutInflater.inflate(LayoutInflater.java:415) 在 com.badass.david.mynewapplication.SelectLogInFragment.onCreateView(SelectLogInFragment.java:16) 在 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.popBackStackState(FragmentManager.java:1670) 在 android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:586) 在 android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:188) 在android.app.Activity.onKeyUp(Activity.java:2576) 在android.view.KeyEvent.dispatch(KeyEvent.java:3171) 在android.app.Activity.dispatchKeyEvent(Activity.java:2831) 在 com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:2438) 在 android.view.ViewRootImpl $ ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4582) 在 android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4537) 在 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 $ AsyncInputStage.forward(ViewRootImpl.java:4201) 在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.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.deliver(ViewRootImpl.java:4068) 在 android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4121) 在 android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4087) 在 android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4234) 在 android.view.ViewRootImpl $ ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4421) 在 android.view.inputmethod.InputMethodManager $ PendingEvent.run(InputMethodManager.java:2480) 在 android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2074) 在 android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2065) 在 android.view.inputmethod.InputMethodManager $ ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2457) 在 android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141) 在android.os.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(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1388) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) 引起:java.lang.IllegalArgumentException:二进制XML文件行
6:复制id 0x7f0c0089,标记为null或父ID为0xffffffff,另一个片段为com.badass.david.mynewapplication.Top_Log
at
android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293) 在android.support.v4.view.LayoutInflaterCompatHC $ Factory
对我来说,它似乎正在尝试创建一个已经存在的片段,因此存在ID冲突。这是问题吗?我该如何解决?
答案 0 :(得分:0)
您应该将文件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>
并在SelectLogInFragment的onCreateView方法中:
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.select_log_in,null);
}
并在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();
}