不能从MainActivity

问题描述:

我有问题时,我想启动一个名为“DetailActivity”活动,当我在列表视图中对项目点击不能从MainActivity

that's my main activity

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

那是我在登录猫错误:

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)  

这里的问题是,我已经声明ID‘weather_detail_container1’在DetailActivity的布局,但是当我使用这个ID的动态添加片段活动无法识别它。

另一件事是,我已经在两个布局activity_detail1.xml(DetailActivity的布局)和activity_main.xml(MainActivity平板电脑的布局)中声明了此ID。

activity_main.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> 

activity_detail1.xml:代码中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> 

位:

package com.example.android.sunshine.app; 

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.Color; 
import android.graphics.PorterDuff; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.FrameLayout; 
import android.widget.LinearLayout; 

public class DetailActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
//  FrameLayout frame = new FrameLayout(this); 
//  frame.setId(R.id.frameFragment); 
//  setContentView(frame, new FrameLayout.LayoutParams(
//    FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); 
     this.setContentView(R.layout.activity_detail1); 
     if(savedInstanceState==null) { 
      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.add(R.id.weather_detail_container1, new DetailActivityFragment()); 
      //fragmentTransaction.addToBackStack(null); 
      fragmentTransaction.commit(); 
     } 

的的LinearLayout您CoordinatorLayout内占据整个屏幕,并没有留下FrameLayout的空间(这是容器哟你正在使用),因为你在高度和宽度都使用“match_parent”。当Android搜索这个视图时,它找不到任何东西,那就是你看到的堆栈跟踪。

要解决这个问题,您应该以显示FrameLayout的方式分割屏幕,即LinearLayout不占用整个屏幕。

指定的错误是:

引起:java.lang.IllegalArgumentException异常:未发现ID 0x7f0f0080 (com.example.android.sunshine.app:id/weather_detail_container1),用于 片段视图DetailActivityFragment {e479b98#1 ID = 0x7f0f0080}

的观点并没有发现!我看到DetailActivity负载布局activity_detail1.xml,但检查是否正在使用其他的布局,对某些定向o定义的名称相同[R密度,例如:

enter image description here

可能他们中的一个doesn't包含weather_detail_container1

+0

我注意到,布局-V21还包括activity_detail1.xml不包含ID:weather_detail_container1,所以我有删除这个文件和目录,干净的项目,但是这个错误仍然发生。 –

+0

什么消息显示在你的logCat中? – Jorgesys

+0

与我的问题相同的消息 –