Reputation: 123
I am new in Android app development and using Java language. My problem is every time I make a TextView or Button there is a triangle with the exclamation mark below them. and when I click it I saw a message saying:
hardcoded string “Button”, should use @string resource
I have two activities, in my main activity there is a Button that when you click it you will go in second activity.
But when I go to my main.java
to make a code for the button. There's always the above shown error. I think the eclipse can't find the id of my button and same for my TextView they have same error message.
Here is the code I made:
Button b = FindViewById(R.id.button1);
I also add:
Button b = (Button) FindViewById(R.id.button1);
I am using the latest eclipse classic and ADT august issue. The platform is Android 4.1 API 16.
Upvotes: 12
Views: 45163
Reputation: 1
In Android Studio, make changes to the String.xml and activity_main.xml files.
In this example you must create a text string in the strings.xml file. strings.xml
And in the activity_main.xml file, you should find the text of the string created, as shown in the example image. Save everything and the error disappears. activity_main.xml
Upvotes: 0
Reputation: 1
I am a newbie too, but I believe I got this. So basically what's happening here, java wants you to put your hardcodes in string.xml. so that when accessing it, you will use the given methods below before:
.
But this is how it should be. Let's start by string.xml
Then come back to your activity_main.xml
Upvotes: 0
Reputation: 193
Notice the id of the button, which is rounded in red. You have to use this id when you want to call it in a method, for an example
Button b = (Button) FindViewById(R.id.button1);
Furthermore, check whether your graphical layout matches with the image I have provided.
Just try your code again with these changes. Your main.java would look like this.
Upvotes: 1
Reputation: 1087
You shouldn't hardcode the "text" on the widgets use the strings resources ie., strings in the strings.xml to set the text. Declare the "text" you want to display as a string in strings.xml and access it using @string/your_string_name in the layout file.
Upvotes: 12