Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Blog Reader Android App Rebuilding from Scratch All About @string Resources

Array error

For the Blog Reader app I just created an array and I get this message in the MainListActivity.java " array cannot be resolved or is not a field ". In the strings.xml I get an error saying " Attribute name "Android" associated with an element type "item" must be followed by the ' = ' character "

error on line 19

MainListActivity.java

package net.kneisel.blogreader;

import android.app.ListActivity; import android.content.res.Resources; import android.os.Bundle; import android.view.Menu; import android.widget.ArrayAdapter;

public class MainListActivity extends ListActivity {

protected String[] mAndroidNames;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_list);

    Resources resources = getResources();
*19*    mAndroidNames = resources.getStringArray(R.array.android_names);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mAndroidNames);
    setListAdapter(adapter);

    //Toast.makeText(this, getString(R.string.no_items), Toast.LENGTH_LONG).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_list, menu);
    return true;
}

}

strings.xml looks fine.. what is the error you're getting in the above code?

1 Answer

Can you post your code from your strings.xml file? Check to make sure your syntax is correct.

Does each item look like this inside the string-array tag?

<item>value</item>

looks like you forgot the closing angle bracket for your item tags

still get an error on line 2: Multiple annotations found at this line: - Attribute name "Android" associated with an element type "item" must be followed by the ' = ' character.

<string-array name ="android_names">
    <item>Android beta</item>
    <item>Android 1.0 </item>
    <item>Android 1.1</item>
    <item>Cupcake</item>
    <item>Donut</item>
    <item>Eclair </item>
    <item>Froyo</item>
    <item>Gingerbread</item>
    <item>Honeycomb </item>
    <item>Ice Cream Sandwhich</item>
    <item>Jelly Bean</item>
    </string-array>

The code you posted in the above comment is correct so the problem is probably somewhere else. Can you post the code for your entire strings.xml file?

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="app_name">BlogReader</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="no_items">No items to display!</string>

<string-array name ="android_names">
    <item>Android beta</item>
    <item>Android 1.0 </item>
    <item>Android 1.1</item>
    <item>Cupcake</item>
    <item>Donut</item>
    <item>Eclair </item>
    <item>Froyo</item>
    <item>Gingerbread</item>
    <item>Honeycomb </item>  
    <item>Ice Cream Sandwhich</item>
    <item>Jelly Bean</item>
    </string-array>

</resources>