2011-09-25 81 views
0

我想在我的Activity的XML布局文件(upgrades.xml)中包含一个包含ListView的ScrollView(upgrade_content.xml)。当我使用<include layout="@layout/upgrades_content"/>包含外部文件时,我得到一个RuntimeException,并显示消息“您必须提供一个layout_width属性。”包含外部xml文件时,LayoutInflater引发RuntimeException:“您必须提供layout_width属性。”

如果我将upgrade_content.xml中的代码复制并直接粘贴到upgrades.xml中,它可以正常工作。

upgrades.xml(这引发异常):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/upgrades_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical" 
    android:background="@drawable/menu_screen_bg" 
    android:orientation="vertical"> 

    <include layout="@layout/upgrades_content"/> 
</LinearLayout> 

upgrades_content.xml:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:fillViewport="true" 
    android:layout_weight="1"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center_horizontal"> 

     <!-- Activity Title --> 
     <RelativeLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingTop="10dp"> 
      <TextView 
       android:id="@+id/upgrades_title_text" 
       android:text="@string/upgrade_store_string" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:textSize="@dimen/TitleTextSize" 
       style="@style/ActivityTitleTextStyle" /> 

     </RelativeLayout> 

     <!-- Upgrades List --> 
     <LinearLayout 
      android:background="@drawable/old_paper_bg" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="@dimen/OldPaperScrollViewMargin" 
      android:layout_marginRight="@dimen/OldPaperScrollViewMargin" 
      android:layout_marginBottom="@dimen/OldPaperScrollViewMargin" 
      android:padding="@dimen/OldPaperScrollViewPadding"> 

      <ListView 
       android:id="@android:id/list" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent"> 

      </ListView> 

     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

UpgradeActivity.java:

public class UpgradeActivity extends ListActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.upgrades); 

     setListAdapter(ArrayAdapter.createFromResource(this, 
       R.array.upgrade_string_array, 
       R.layout.upgrade_layout_list_item)); 
    } 
} 

upgrade_layout_list_item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="@dimen/LabelTextSize" /> 

编辑: 这段代码不会导致强制关闭抛出的RuntimeException的,所以我可能刚才已经有点过于宽松,其中例外我设置的Eclipse赶上。

+0

您可以显示包括布局吗? – 2011-09-25 22:35:23

+0

请确保您在ScrollView布局中声明了layout_width –

+0

对不起,在放入一些标签时,我不小心按下了Enter键,因此它在我写完之前问了问题。现在完成了。 – djdilicious

回答

0

尝试指定布局的高度和宽度在upgrade.xml<include>标签下是这样的:

<include 
    layout="@layout/upgrades_content" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

把宽度和高度值,无论填补你的需要:fill_parentwrap_content

+0

我曾尝试过,它仍然会引发异常。如果我将Eclipse设置为不捕获RuntimeExceptions,则该应用程序不会强制关闭,因此它不应该成为问题。谢谢。 – djdilicious

+0

我刚刚试过你的代码,没有你设置的样式和尺寸,因为我没有你的'string.xml'文件。所以我尝试使用常见的文本大小,边距值,并且为了测试目的还删除了背景色,并且代码在这里工作正常,没有出现任何异常。那么你会在这里给你的'string.xml'吗?我也会测试你的价值观,看看出了什么问题。 –

+0

谢谢,我想我只是捕获所有的RuntimeExceptions而不是仅仅是未捕获的异常。该应用程序不会崩溃,所以我认为该异常是由Android成功捕获和处理的。 – djdilicious

相关问题