Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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>