2012-07-05 57 views
2

我一直在使用elcipse几个星期了。我的代码功能正常,但突然间它已停止工作。我得到这个错误,Android错误:您必须指定有效的布局参考。布局ID。在elcipse

main.xml:你必须指定一个有效的布局引用。布局ID @ layout/required无效。

我的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="fill_parent" > 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 


    <!-- select between BIN or auction --> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal" > 

    <Button 
     android:id="@+id/AuctionButton" 
     android:layout_marginLeft="82dp" 
     android:layout_width="119dp" 
     android:layout_height="wrap_content" 
     android:onClick="showAuctionOptions" 
     android:text="Auction" /> 


    <Button 
     android:id="@+id/BINButton" 
     android:layout_marginLeft="-2dp" 
     android:layout_width="119dp" 
     android:layout_height="wrap_content" 
     android:onClick="showAuctionOptions" 
     android:text="Buy it now" /> 

    </LinearLayout> 

     <include android:id="@+id/cell1" layout="@layout/required"/> 

     <include android:id="@+id/cell2" layout="@layout/gray_line" /> 

     <!--** Show if Buy it now is selected **--> 
     <include android:id="@+id/cell3" layout="@layout/buyitnow" /> 
     <!-- ** End Buy it now display ** --> 

     <!--** Show if auction selected ** --> 
     <include android:id="@+id/cell4" layout="@layout/auction" /> 

     <include android:id="@+id/cell5" layout="@layout/gray_line" /> 

     <include android:id="@+id/cell6" layout="@layout/addons" /> 

     <include android:id="@+id/cell7" layout="@layout/calculate" />  

</LinearLayout> 

</ScrollView> 

误差与包括第一,但是如果我删除,我只是得到与第二错误包括。如果我删除所有包含,我会在我的java代码中得到'无法解决R'错误。

我都试过了,

  • 清洁项目
  • 。重新启动Eclipse
  • 重新启动我的电脑

这是我的AndroidManifest XML文件,

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="toggler.state" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name"> 
     <activity 
      android:name=".TogglerActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

无其中已经工作了。有谁可以帮忙吗?

感谢

+0

@ layout/required是布局文件的布局ID或名称res/layout/required.xml ??? – 2012-07-05 15:28:47

+0

很好的思考问题+1。 – ninge 2012-07-05 16:50:00

回答

3

的错误可能是在其他地方的布局造成的。删除多余的两个xmlns:android="http://schemas.android.com/apk/res/android"

xmlns用于定义名称空间。在大多数情况下,只有最上面的元素才能拥有它。

看看What does "xmlns" in XML mean?是否有人想了解关于xmlns的解释。

另请检查以确保您的XML文件全部为小写。

也尝试查看您的清单中是否指定了正确的包。从https://stackoverflow.com/a/7496766/935779

It is worth checking in AndroidManifest.xml . The attribute package has the correct value.

That is, <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.correct.package.name" ...

After you change that, the R.java will be re-generated.

<?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="fill_parent" > 


    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 


     <!-- select between BIN or auction --> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:orientation="horizontal" > 

     <Button 
      android:id="@+id/AuctionButton" 
      android:layout_marginLeft="82dp" 
      android:layout_width="119dp" 
      android:layout_height="wrap_content" 
      android:onClick="showAuctionOptions" 
      android:text="Auction" /> 


     <Button 
      android:id="@+id/BINButton" 
      android:layout_marginLeft="-2dp" 
      android:layout_width="119dp" 
      android:layout_height="wrap_content" 
      android:onClick="showAuctionOptions" 
      android:text="Buy it now" /> 

     </LinearLayout> 

     <include android:id="@+id/cell1" layout="@layout/required"/> 

     <include android:id="@+id/cell2" layout="@layout/gray_line" /> 

     <!--** Show if Buy it now is selected **--> 
     <include android:id="@+id/cell3" layout="@layout/buyitnow" /> 
     <!-- ** End Buy it now display ** --> 

     <!--** Show if auction selected ** --> 
     <include android:id="@+id/cell4" layout="@layout/auction" /> 

     <include android:id="@+id/cell5" layout="@layout/gray_line" /> 

     <include android:id="@+id/cell6" layout="@layout/addons" /> 

     <include android:id="@+id/cell7" layout="@layout/calculate" />  

    </LinearLayout> 

</ScrollView> 
+0

完成,但我仍然收到错误。 – rusty009 2012-07-05 15:31:11

+0

再次尝试清理该项目,确保使用小写的XML文件名或强制重新生成“R”值。我已经添加了一些答案。 – Kirk 2012-07-05 15:38:54

+0

再次感谢您在删除xmlns后清理了该项目,但仍然遇到同样的问题。 Android清单看起来对我来说很好,我已经将它放到上面的答案中,以防万一出现错误。 – rusty009 2012-07-05 16:30:48

0

两者我有一个类似的问题,并能够通过提供layout_width和layout_height到包括标签即可修复。