【发布时间】:2018-09-01 20:36:33
【问题描述】:
以下是有关异常的信息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.v.layout, PID: 13198
android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #0: Duplicate id 0x7f070029, tag null, or parent id 0xffffffff with another fragment for app.v.layout.contentFragment
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3680)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at app.v.layout.MainActivity.onConfigurationChanged(MainActivity.java:21)
at android.app.ActivityThread.performActivityConfigurationChanged(ActivityThread.java:4942)
at android.app.ActivityThread.performConfigurationChangedForActivity(ActivityThread.java:4809)
at android.app.ActivityThread.performConfigurationChangedForActivity(ActivityThread.java:4787)
at android.app.ActivityThread.handleActivityConfigurationChanged(ActivityThread.java:5134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1728)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
MainActivity的代码如下:
package app.v.layout;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.activity_main_land);
}else{
setContentView(R.layout.activity_main);
}
}
}
还有两个xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimary"
tools:context="app.v.layout.MainActivity">
<fragment
class="app.v.layout.contentFragment"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</fragment>
<fragment
class="app.v.layout.InteractionFragment"
android:id="@+id/interaction"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</fragment>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/colorPrimary"
tools:context="app.v.layout.MainActivity">
<fragment
android:id="@+id/content2"
class="app.v.layout.contentFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</fragment>
<fragment
android:id="@+id/interaction2"
class="app.v.layout.InteractionFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</fragment>
</LinearLayout>
当我点击屏幕时,它第一次运行良好,但随后它就会停止。我想知道为什么。有没有办法解决这个问题?提前致谢。
其实我想切换的两个xml并没有太大区别。如果我可以调整片段的属性,比如高度或宽度,那么我将不需要使用函数:setContentView。
Ps:我已经添加了以下行: android:configChanges="orientation|screenSize",然而,正如我之前所说,它只会工作一次,然后突然停止。 AndroidManifest.xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.v.layout">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
【问题讨论】:
-
检查this
-
你能把它的 github 链接发给我吗?
-
@AbhishekTiwari 谢谢,这是链接:(我上传了 src 文件和描述问题的 gif,够吗?)github.com/Vdeus/Android