2天前,XML Layout运行完美,现在(没有结构变化)它给了我错误?

时间:2015-06-20 00:21:30

标签: android xml android-activity runtime-error android-support-library

两天前,我发布了the following question。虽然有问题,但它仍然运行 - 我仍然可以在手机上测试代码。但是,如果代码没有重大更改(并且绝对没有结构更改),程序将在我进入第二个活动并崩溃时拒绝运行。

幸运的是,我已在链接问题中发布了该活动的所有XML代码。

错误(实际警告)

  1. Element fragment is not allowed here
  2. Element ImageView is not allowed here
  3. 这怎么可能?这是我在手机上运行的代码,现在它给了我一长串的错误消息,我无法运行我的第二个Activity,但我可以运行我的第一个活动。

    详细

    This inspection highlights unallowed XML tags in Android resource files and AndroidManifest.xml
    

    额外信息

    1. 自发布链接问题
    2. 后,我将我的IDE从Android Studio 1.3 preview 4更新为preview 5

      代码

      Ran 2天前

      <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true">
      
          <android.support.design.widget.AppBarLayout
              android:id="@+id/appbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
      
              <android.support.design.widget.CollapsingToolbarLayout
                  android:id="@+id/collapsing_toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  app:contentScrim="?attr/colorPrimary"
                  app:expandedTitleMarginEnd="16dp"
                  app:expandedTitleMarginStart="48dp"
                  app:layout_scrollFlags="scroll|enterAlways">
      
                  <ImageView
                      android:id="@+id/header"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:background="@drawable/download"
      
                      android:scaleType="centerCrop" />
      
                  <android.support.v7.widget.Toolbar
                      android:id="@+id/anim_toolbar"
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
      
                      app:layout_collapseMode="pin" />
      
              </android.support.design.widget.CollapsingToolbarLayout>
      
          </android.support.design.widget.AppBarLayout>
      
          <android.support.v4.widget.NestedScrollView
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_below="@+id/anim_toolbar"
              app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
              <fragment
                  android:id="@+id/detail"
                  android:name="<package>.<fragment_name>"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" />
      
          </android.support.v4.widget.NestedScrollView>
      
      </android.support.design.widget.CoordinatorLayout>
      

      今天不工作

      <?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"
          android:fitsSystemWindows="true">
      
          <android.support.design.widget.AppBarLayout
              android:id="@+id/appbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:fitsSystemWindows="true">
      
              <android.support.design.widget.CollapsingToolbarLayout
                  android:id="@+id/collapsing_toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  app:contentScrim="?attr/colorPrimary"
                  app:expandedTitleMarginEnd="48dp"
                  app:expandedTitleMarginStart="48dp"
                  app:layout_scrollFlags="scroll|exitUntilCollapsed"
                  android:fitsSystemWindows="true">
      
                  <!-- HERE....ERROR -->
                  <ImageView  
                      android:id="@+id/header"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:background="@drawable/download"
                      android:scaleType="centerCrop"
                      app:layout_collapseMode="parallax"
                      android:contentDescription="Movie Poster"
                      android:fitsSystemWindows="true"/>
      
                  <android.support.v7.widget.Toolbar
                      android:id="@+id/anim_toolbar"
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
                      android:fitsSystemWindows="true" />
      
              </android.support.design.widget.CollapsingToolbarLayout>
      
          </android.support.design.widget.AppBarLayout>
      
          <android.support.v4.widget.NestedScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/scrollView"
              android:layout_below="@+id/anim_toolbar"
              app:layout_behavior="@string/appbar_scrolling_view_behavior" >
      
                  <!-- HERE....ERROR -->
                  <fragment
                      android:id="@+id/movie_detail"
                      android:name="com.udacity.rucinskic.spotify_streamer.movies.fragments.MovieOverviewFragment"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      tools:layout="@layout/fragment_movie_detail" />
      
          </android.support.v4.widget.NestedScrollView>
      
      </android.support.design.widget.CoordinatorLayout>
      

      OnCreateView

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
      
          View rootView = inflater.inflate(R.layout.fragment_movie_poster, container, false);
      
          TextView relaseYear = (TextView) rootView.findViewById(R.id.movie_release_year);
          RatingBar rating = (RatingBar) rootView.findViewById(R.id.movie_rating);
          TextView overview = (TextView) rootView.findViewById(R.id.movie_overview);
      
          DetailedMovie movie = getMovie();
      
          DateFormat format = new SimpleDateFormat("yyyy", Locale.US);
      
          relaseYear.setText(format.format(movie.getDateRelease()));
          rating.setRating(movie.getRating());
          overview.setText(movie.getOverview());
      
          return rootView;
      
      }
      

      修改

      堆栈跟踪

          Process: com.udacity.rucinskic.spotify_streamer, PID: 27320
          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.udacity.rucinskic.spotify_streamer/com.udacity.rucinskic.spotify_streamer.SecondaryActivity}: android.view.InflateException: Binary XML file line #66: Error inflating class fragment
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2712)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2777)
                  at android.app.ActivityThread.access$900(ActivityThread.java:179)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:145)
                  at android.app.ActivityThread.main(ActivityThread.java:5972)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
           Caused by: android.view.InflateException: Binary XML file line #66: Error inflating class fragment
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
                  at com.udacity.rucinskic.spotify_streamer.SecondaryActivity.onCreate(SecondaryActivity.java:21)
                  at android.app.Activity.performCreate(Activity.java:6289)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2777)
                  at android.app.ActivityThread.access$900(ActivityThread.java:179)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:145)
                  at android.app.ActivityThread.main(ActivityThread.java:5972)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
           Caused by: java.lang.IllegalArgumentException: Bad class: class java.util.GregorianCalendar
                  at java.text.DateFormat.format(DateFormat.java:296)
                  at java.text.Format.format(Format.java:93)
                  at com.udacity.rucinskic.spotify_streamer.movies.fragments.MovieOverviewFragment.onCreateView(MovieOverviewFragment.java:35)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:924)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116)
                  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1218)
                  at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2170)
                  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
                  at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:838)
                  at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:34)
                  at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:826)
                  at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:732)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
                  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
                  at com.udacity.rucinskic.spotify_streamer.SecondaryActivity.onCreate(SecondaryActivity.java:21)
                  at android.app.Activity.performCreate(Activity.java:6289)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2777)
                  at android.app.ActivityThread.access$900(ActivityThread.java:179)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:145)
                  at android.app.ActivityThread.main(ActivityThread.java:5972)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
      

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。尝试将ImageView交换到android.support.v7.widget.AppCompatImageView。它解决了我的问题!

答案 1 :(得分:0)

尝试:

  • 在APP MODULE中设置新建的工具
  • 在项目中设置新的插件
  • 检查支持图书馆的依赖
  • 无效CACHE

项目 enter image description here

app模块 enter image description here

支持app模块中的依赖项 enter image description here 我自己尝试过&amp;我没有任何问题

enter image description here

答案 2 :(得分:0)

我有同样的问题。首先关闭您的项目,然后再次导入。它解决了我的问题。