I am trying to make a Hello World app. Here is the link to the tutorial I am using.
I have successfully gotten through the “Creating the User Interface with Code” and seen the app work in the emulator but when I got to “Creating String Resources” I ran into some trouble. I changed my Strings.xml file to:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="helloButtonText">Say Hello</string>
<string name="helloLabelText">Hello Mono for Android</string>
</resources>
as it says to do, then I changed the lines in my Activity1.cs so it is:
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);
}
};
}
}
}
Then when I try and run, I get the error:
no resource found that matches the given name (at ‘text’ with value ‘@string/hello’)
which it says it is at line 2 of Main.axml so here is that code:
<?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>
I have tried other Android tutorials and I seem to always get stuck at the part where I add something to the Strings.xml file. I solution to this problem would be greatly appreciated.
The last attribute on your button’s XML (that is,
android:text="@string/hello") is attempting to set the text to the value of the string resourcehello. You have not defined a string resource namedhelloin your strings.xml file. You need to either define one, or use a different one, such ashelloButtonText:Alternatively, since you appear to be setting your views programmatically rather than via the XML you’ve defined, you could just get rid of the XML layout file entirely for now. You don’t appear to be using it anywhere.