无法从MainActivity启动DetaiActivity

时间:2017-09-25 22:21:10

标签: android

当我想要名为" DetailActivity"当我点击listview中的项目时

that's my main activity

所以当我点击listview中的项目时,我应该看到DetailActivity。以前,当以静态方式声明DetailActivity的片段时,将打开DetailActivity,但是当我将其更改为动态方式时(通过调用getSupportFragment()。beginTransaction()。add(.....)),DetailActiviy不再启动。

这是我在Log Cat中的错误:

DataType

这里的问题是我已经声明了id" weather_detail_container1"在DetailActivity的布局中,但是当我使用此id动态添加片段时,活动无法识别它。

另一件事是我在两个布局中声明了这个id:activity_detail1.xml(DetailActivity的布局)和activity_main.xml(平板电脑的MainActivity布局)。

平板电脑的

activity_main.xml:

09-24 11:01:37.986 9114-9114/com.example.android.sunshine.app E/FragmentManager: No view found for id 0x7f0f0080 (com.example.android.sunshine.app:id/weather_detail_container1) for fragment DetailActivityFragment{e479b98 #1 id=0x7f0f0080}
09-24 11:01:37.986 9114-9114/com.example.android.sunshine.app E/FragmentManager: Activity state:
09-24 11:01:38.003 9114-9114/com.example.android.sunshine.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.android.sunshine.app, PID: 9114
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.sunshine.app/com.example.android.sunshine.app.DetailActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0f0080 (com.example.android.sunshine.app:id/weather_detail_container1) for fragment DetailActivityFragment{e479b98 #1 id=0x7f0f0080}
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                    at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                    at android.os.Looper.loop(Looper.java:164)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6540)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                                 Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0f0080 (com.example.android.sunshine.app:id/weather_detail_container1) for fragment DetailActivityFragment{e479b98 #1 id=0x7f0f0080}
                                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1326)
                                                                                    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
                                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
                                                                                    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
                                                                                    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415)
                                                                                    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200)
                                                                                    at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153)
                                                                                    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2063)
                                                                                    at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                                    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:554)
                                                                                    at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
                                                                                    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1333)
                                                                                    at android.app.Activity.performStart(Activity.java:6997)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2780)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                                    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                    at android.os.Looper.loop(Looper.java:164) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6540) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

activity_detail1.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    tools:context="com.example.android.sunshine.app.MainActivity">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        >
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:contentInsetLeft="16dp"
                app:contentInsetStart="16dp"
                app:contentInsetEnd="16dp"
                app:titleMarginStart="32dp"

                />

        </android.support.design.widget.AppBarLayout>

        <!--<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"-->
        <!--xmlns:tools="http://schemas.android.com/tools"-->
        <!--android:id="@+id/container"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="wrap_content"-->
        <!--tools:context="com.example.android.sunshine.app.MainActivity"-->
        <!--android:background="@android:color/white"-->
        <!--tools:ignore="MergeRootFrame"-->
        <!--&gt;-->

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false"
            android:divider="?android:attr/dividerHorizontal"
            android:orientation="horizontal"
            tools:context="com.example.android.sunshine.app.MainActivity">


            <fragment android:name="com.example.android.sunshine.app.MainActivityFragment"
                android:id="@+id/list"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                tools:layout="@android:layout/list_content"
                />
            <FrameLayout
                android:id="@+id/weather_detail_container1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="4" />
        </LinearLayout>
        <!--</FrameLayout>-->
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>
DetailActivity.java中的一些代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    tools:context="com.example.android.sunshine.app.DetailActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        tools:context="com.example.android.sunshine.app.DetailActivity"
        >
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            >

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_detail"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:titleMarginStart="32dp"/>

                <!--<ImageView-->
                    <!--android:layout_height="24dp"-->
                    <!--android:layout_width="24dp"-->
                    <!--android:maxWidth="16dp"-->
                    <!--android:maxHeight="16dp"-->
                    <!--android:src="@drawable/sunshine_logo"-->
                    <!--android:gravity="left"-->
                    <!--/>-->

            <!--</android.support.v7.widget.Toolbar>-->

        </android.support.design.widget.AppBarLayout>

        <!--<fragment xmlns:android="http://schemas.android.com/apk/res/android"-->
            <!--xmlns:tools="http://schemas.android.com/tools"-->
            <!--android:id="@+id/fragment"-->
            <!--android:name="com.example.android.sunshine.app.DetailActivityFragment"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="match_parent"-->
            <!--tools:layout="@layout/fragment_detail" />-->
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/weather_detail_container1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.example.android.sunshine.app.DetailActivity"
            tools:ignore="MergeRootFrame" />

    </LinearLayout>


</android.support.design.widget.CoordinatorLayout>