【问题标题】:no resourse found that matches the given name (at 'text' with value '@string/hello')找不到与给定名称匹配的资源(在“文本”处,值为“@string/hello”)
【发布时间】:2012-06-07 16:05:30
【问题描述】:

我正在尝试制作一个 Hello World 应用程序。这是tutorial I am using的链接。

我已成功通过“使用代码创建用户界面”并看到该应用程序在模拟器中运行,但是当我进入“创建字符串资源”时遇到了一些麻烦。我将我的 Strings.xml 文件更改为:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="helloButtonText">Say Hello</string>
        <string name="helloLabelText">Hello Mono for Android</string>
    </resources> 

正如它所说的那样,然后我更改了我的 Activity1.cs 中的行,所以它是:

    using System;
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;

    namespace HelloM4A
{
[Activity (Label = "HelloM4A", MainLauncher = true)]
public class Activity1 : Activity
{
    int count = 1;

protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        //Create the user interface in code
        var layout = new LinearLayout (this);
        layout.Orientation = Orientation.Vertical;

        var aLabel = new TextView (this);
        //old line aLabel.Text = "Hello, Mono for Android";
        aLabel.SetText (Resource.String.helloLabelText);

        var aButton = new Button (this);     
        //old line aButton.Text = "Say Hello";
        aButton.SetText (Resource.String.helloButtonText);

    aButton.Click += (sender, e) => {
        aLabel.Text = "Hello from the button";
    }; 
    layout.AddView (aLabel);
    layout.AddView (aButton);          
    SetContentView (layout);
    }
        };
    }
}

}

然后,当我尝试运行时,我收到错误消息: 找不到与给定名称匹配的资源(在“文本”处,值为“@string/hello”) 它说它在 Main.axml 的第 2 行,所以这里是代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
    android:id="@+id/myButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
</LinearLayout>

我尝试过其他 Android 教程,但似乎总是卡在向 Strings.xml 文件添加内容的部分。我对此问题的解决方案将不胜感激。

【问题讨论】:

  • 欢迎来到 SO。如果以下答案之一解决了您的问题,您应该接受它(单击相应答案旁边的复选标记)。这有两件事。它让每个人都知道您的问题已得到解决,并让帮助您的人获得帮助。

标签: android


【解决方案1】:
<Button
    android:id="@+id/myButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

按钮 XML 上的最后一个属性(即android:text="@string/hello")试图将文本设置为字符串资源hello 的值。您尚未在 strings.xml 文件中定义名为 hello 的字符串资源。您需要定义一个,或者使用不同的,例如helloButtonText

<Button
    android:id="@+id/myButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/helloButtonText" />

另外,由于您似乎是以编程方式设置视图,而不是通过您定义的 XML,您现在可以完全摆脱 XML 布局文件。您似乎没有在任何地方使用它。

【讨论】:

  • 感谢您的帮助。这种变化很有意义。
【解决方案2】:

您的问题是它(hello 字符串)不存在。您创建了helloButtonTexthelloLabelText,但没有创建hello

将您的按钮 xml 更改为此,它应该可以工作:

<Button 
    android:id="@+id/myButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/helloButtonText" /> 

【讨论】:

  • 感谢您的帮助。我似乎无法将两个答案都标记为解决方案,但您的答案与 eldarerathis 相同,因此您的解决方案有效。
  • 没关系。我只是想确保你知道接受。一个答案。很多新人不这样做,最终人们不帮助他们,因为他们不接受给出的答案。
猜你喜欢
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
相关资源
最近更新 更多