错误:找不到与给定名称匹配的资源

5
在这个文件中,我的布局出现了以下错误。
error: Error: No resource found that matches the given name (at 'id' with value '@id/question_text_view').
error: Error: No resource found that matches the given name (at 'id' with value '@id/next_button').

这是一个布局文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >
        <Button 
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button"
        />
        <Button 
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button"
        />
    </LinearLayout>
        <Button 
        android:id="@id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next_button"
        />
</LinearLayout>

这是我的strings.xml文件。
<?xml version="1.0" encoding="utf-8"?>
    <resources>

        <string name="app_name">GeoQuiz</string>
        <string name="true_button">True</string>
        <string name="false_button">False</string>
        <string name="correct_toast">Correct</string>
        <string name="next_button">Next</string>
        <string name="incorrect_toast">Incorrect</string>
    <string name="action_settings">Settings</string>
    <string name="question_founder">Frank Reed Horton Founded APO on december 25 1947</string>
    <string name="question_chief">Willy Wonka was known as a cheif</string>
    <string name="question_lady">The first lady that became president in APO was Mary Katz</string>
    <string name="question_president">Our current President of the Delta Rho chapter is  john dolan</string>
<string name="question_alphabets">Alpha, Beta, Gamma, Delta, Epsilon, Eta, Zeta</string>
</resources>

我知道字符串中没有question_text_view,但是我在我的活动中创建了一个数组来存储所有的问题。 mQuestionTextView =(TextView)findViewById(R.id.question_text_view); int question = mQuestionBank [mCurrentIndex] .getQuestion(); mQuestionTextView.setText(question); mQuestionBank是我提出所有问题的数组 getQuestion()是我用来获取问题的getter方法

对于next_button的第二个错误,我不知道我做错了什么,因为我已经把它包含在strings.xml中了。 有人可以帮忙解决吗?


1
android:id="@id/question_text_view" 应该改为 android:id="@+id/question_text_view",按钮也是一样的,android:id="@+id/next_button" 应该改为 android:id="@+id/next_button"。 - Raghunandan
1
请查看更多资源类型,以了解在xml中定义视图id的方法。您需要使用<item type="id" name="Next" />而不是<string />将id放置在strings.xml文件中,或者您可以创建单独的文件ids.xml来存储它们。 - ρяσѕρєя K
3个回答

8
"

android:id="@id/question_text_view" 应该改为 android:id="@+id/question_text_view",同样的,button 的 android:id="@id/next_button" 应该改为 android:id="@+id/next_button"

TextView

"
    <TextView
    android:id="@+id/question_text_view" //  missing + in your xml code
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="24dp"
    />

按钮
    <Button 
    android:id="@+id/next_button"  // missing + in your xml code
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/next_button"
    /> 

ID

任何视图对象都可以有一个整数ID与之关联,以在树中唯一标识该视图。当应用程序被编译时,此ID被引用为整数,但通常在布局XML文件中作为字符串分配给ID属性。这是所有View对象(由View类定义)共有的XML属性,您将经常使用它。在XML标记内部,ID的语法如下:

字符串开头的“@”符号表示XML解析器应解析并展开其余的ID字符串,并将其标识为ID资源。“+”符号表示这是一个新的资源名称,必须创建并添加到我们的资源中(在R.java文件中)。

在您的情况下,您认为按钮的ID为android:id = "@id/next_button"。您正在引用Android资源ID(假设)。这不存在。对于textview也是如此。因此,您会收到找不到资源的错误。

有关更多信息,请查看以下链接

http://developer.android.com/guide/topics/ui/declaring-layout.html


0

你需要注意视图的ID。在TextView和Button视图中,你忘记了ID属性中的“+”号。据我所知,你试图使用该ID引用另一个视图。如果你使用“+”,那么表示你为该视图创建了一个新的ID。

已修复版本:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >
        <Button 
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button"
        />
        <Button 
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button"
        />
    </LinearLayout>
        <Button 
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next_button"
        />
</LinearLayout>

0

也许文件夹路径太长了,就像我的:"E:\Project\Mobile\SixworldGit\sixworld\projects\sixworld\SW_Vietnam\2.3.2.0.20160711_Android_MoboSDKAll_lucgioi_20160711_1805\2.3.2.0.20160711_Android_MoboSDKAll\MoboSDK\reference\MoboSDKAll\res\drawable"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接