以下是有关例外的信息:
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);
}
}
}
两个xmls:
<?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>
当我对屏幕进行调整时,它第一次运行良好,但之后就会停止。我想知道为什么。有没有办法解决这个问题?提前谢谢。
事实上,我想切换的两个xmls之间没有太大的区别。如果我只能调整片段的属性,比如高度或宽度,那么,我就不需要使用函数:setContentView。
Ps:我已经添加了以下行: android:configChanges =&#34; orientation | screenSize&#34;但是,正如我之前提到的那样,它只会在它突然停止之前工作一次。 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>
答案 0 :(得分:0)
使用此更新您的清单文件并尝试
<activity
android:name="com.example.test.activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>
Android将自行管理以避免娱乐。
答案 1 :(得分:0)
您需要收听已经完成的屏幕旋转配置更改。但是您需要使用
将其明确声明到特定活动的清单中android:configChanges="orientation|screenSize"
答案 2 :(得分:0)
转到此链接以获取有关的信息 https://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
为旋转放置不同的布局,屏幕尺寸根据:
更改res / layout / layout.xml // app的默认布局
res / layout-land / layout.xml //旋转时的布局。
res / layout-w600dp / layout //当屏幕宽度增加600dp限制时的布局
答案 3 :(得分:0)
感谢所有人愿意提供帮助。 我的问题刚刚解决了,它是关于函数“setContentView()”的东西。 (我不知道究竟在哪里,任何详细说明都会受到赞赏。)但我确实设法通过更改有关该功能的几行来实现:
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.activity_main,null);
viewLand=inflater.inflate(R.layout.activity_main_land,null);
和“onConfigurationChanged()”:
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
setContentView(viewLand);
}else{
setContentView(view);
}
总而言之,不能在“setContentView()”中使用相同的“R.layout.activity_main”两次。我希望这会帮助那些不幸遇到同样问题的人。