2014-11-04 30 views
3

而在圖形佈局檢查我收到此錯誤:下面我貼我的activity_main.xml中碼MockView不能轉換爲android.view.ViewGroup在XML

Exception raised during rendering: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup

activity_main .XML:

<?xml version="1.0" encoding="utf-8"?> 

    <android.support.v4.widget.DrawerLayout 

    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <RelativeLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TextView 
      android:id="@+id/textviewHeading" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="5dp" 
      android:fontFamily="fonts/Dosis.otf" 
      android:text="@string/heading_chapter" 
      android:textSize="25dp" 
      android:visibility="gone" /> 

     <ListView 
      android:id="@+id/listviewChapters" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_above="@+id/imageviewBanner" 
      android:layout_below="@+id/textviewHeading" 
      android:layout_marginTop="5dp" > 
     </ListView> 

     <ImageView 
      android:id="@+id/imageviewBanner" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:src="@drawable/banner" 
      android:visibility="invisible" /> 
    </RelativeLayout> 

    <ListView 
     android:id="@+id/list_slidermenu" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@color/list_divider" 
     android:dividerHeight="1dp"   
     android:listSelector="@drawable/list_selector" 
     android:background="@color/list_background"/> 

    </android.support.v4.widget.DrawerLayout> 
+0

您是否重新啓動IDE? – 2014-11-04 10:32:59

+0

yes.But仍然錯誤保持不變。 – Steve 2014-11-04 10:35:45

+0

是否可以在DrawerLayout中添加兩個以上的子視圖? @Alif – Steve 2014-11-04 10:37:30

回答

2

由於抽屜佈局的錯誤實現而發生此錯誤。 我將三個直接佈局放置到抽屜佈局上,錯誤會發生。根據Google文檔,它直接採用了這兩種佈局。

在抽屜佈局中,如果我放置兩個直接佈局解決了問題。這就是爲什麼我使用框架佈局作爲抽屜佈局下面的父佈局,然後最後添加一個列表視圖來顯示導航抽屜。

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout 
     android:id="@+id/frame_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <RelativeLayout 
      android:id="@+id/container" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" > 

      <TextView 
       android:id="@+id/textviewHeading" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="5dp" 
       android:fontFamily="fonts/Dosis.otf" 
       android:text="@string/heading_chapter" 
       android:textSize="25sp" 
       android:visibility="gone" /> 

      <ListView 
       android:id="@+id/listviewChapters" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_above="@+id/imageviewBanner" 
       android:layout_below="@+id/textviewHeading" 
       android:layout_marginTop="5dp" > 
      </ListView> 

      <ImageView 
       android:id="@+id/imageviewBanner" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" 
       android:src="@drawable/banner" 
       android:visibility="invisible" /> 
     </RelativeLayout> 
    </FrameLayout> 

    <ListView 
     android:id="@+id/list_slidermenu" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@color/list_background" 
     android:choiceMode="singleChoice" 
     android:divider="@color/list_divider" 
     android:dividerHeight="1dp" 
     android:listSelector="@drawable/list_selector" /> 

</android.support.v4.widget.DrawerLayout> 
0

使用本

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:id="@+id/drawer_layout" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" > 
 

 
    <FrameLayout 
 
     android:id="@+id/container" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     tools:context="com.sun_soft.app_maker_new.MainActivity" 
 
     tools:ignore="MergeRootFrame" /> 
 

 
    <ListView 
 
     android:id="@+id/left_drawer" 
 
     android:layout_width="240dp" 
 
     android:layout_height="match_parent" 
 
     android:layout_gravity="start" 
 
     android:background="#ffffff" 
 
     android:choiceMode="singleChoice" 
 
     android:divider="@android:color/transparent" 
 
     android:dividerHeight="0dp" /> 
 

 
    <ListView 
 
     android:id="@+id/right_drawer" 
 
     android:layout_width="@dimen/paanelsize" 
 
     android:layout_height="match_parent" 
 
     android:layout_gravity="end" 
 
     android:background="#ffffff" 
 
     android:choiceMode="singleChoice" 
 
     android:divider="@android:color/transparent" 
 
     android:dividerHeight="0dp" /> 
 

 
</android.support.v4.widget.DrawerLayout>

再後來你要參考列表視圖中mainactivity和必須做出適配器和膨脹視圖列表項。

我認爲在你的代碼中,你錯誤地投下了一些看法。 請郵編。

+0

Android尚不支持Android XML,並且您的代碼片段不會在此處執行。 – mr5 2015-08-25 15:12:52

0

我會建議你檢查在佈局引用的資源。

在我的情況下,我通過將psd文件保存爲png文件而犯了一個錯誤。 當這個「壞」的png文件在我的佈局中引用時,Eclipse結束了這樣的錯誤,即

Exception raised during rendering: com.android.layoutlib.bridge.MockView cannot be cast to android.widget.ImageView 

只是因爲在ImageView的引用的PNG文件,事實上,PSD文件,即:

android:src="@drawable/my_psd_file_with_wrong_png_extension" 

希望這會有所幫助。

2

我發現這個問題到我的項目中,然後我將support.v4.jar文件添加到我的項目中,然後解決我的問題,所以我認爲你可以試試這個。

+0

我很早就找到了答案,併發布它。可能是你的答案會幫助別人:) – Steve 2015-02-12 10:49:03

相關問題