当我旋转屏幕时,我想更改 主活动 的布局,其中包含两个片段



以下是有关异常的信息:

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>

用这个更新你的清单文件并尝试,

<activity
android:name="com.example.test.activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>

Android将自我管理以避免娱乐。

您需要侦听屏幕旋转的配置更改,您已经完成了。但是您需要在特定活动中明确声明到您的清单中

,使用
android:configChanges="orientation|screenSize"

转到此链接以获取有关https://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources

放置不同的布局进行旋转,屏幕尺寸根据以下情况变化:

res/layout

/layout.xml//app 的默认布局

res/layout-land/layout.xml//layout(旋转时(。

res/layout-w600dp/layout//layout 当屏幕宽度增加 600dp 限制时

感谢大家愿意提供帮助。我的问题刚刚解决了,这是关于函数"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"。我希望这将帮助那些不幸遇到与我相同问题的人。

相关内容

最新更新