سؤال

So, I'm developing with Android. I'd like to know, what is wrong with this code? On activity_main.xml in the res/layout directory, in the Graphical Layout editor, it gives me this error:

 Couldn't resolve resource @string/edit_message
 Couldn't resolve resource @string/button_send

Strings:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">My First App</string>
  <string name="edit_message">Enter a message</string>
  <string name="button_send">Send</string>
  <string name="action_settings">Settings</string>
  <string name="title_activity_main">MainActivity</string>
</resources>

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
</LinearLayout>
هل كانت مفيدة؟

المحلول

Clean your project and check it out. there is no problem in your code or xml..

project->clean

نصائح أخرى

cut this code in your mainactivity.xml :

<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>

and put it to res/values/strings.xml

You've got two root elements in your XML document. Your String resources should go in a file separate to your layout document, usually the file is called "strings.xml.", which would appear in the "values" directory of your project.

Also, you've given your EditText element an id with the name "edit_message". This could clash with your string named "edit_message".

Check if the "fragment_main.xml" and the "strings.xml" tab have a "*" on it, like this "*strings.xml". It means that you made changes and haven't saved them yet, click on (File > Save) and that's it. At least worked for me.

Make sure you string references name are spaced correctly in activity xml and Android manifest files them it to be parsed and rendered properly as separate tokens.e.g @string/buttonX or @+id/string/buttonX are wrong instead space them to " @ string/buttonX " or " @ + id/buttonX "

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top