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 Adding Data for the List

unreachable code while using adapter

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

when i enter the above mentioned code, i get an error message unreachable code. Please help me regarding this error

YUCHIH YU
YUCHIH YU
6,364 Points

Can you post some snippet of yours. Probably you have some return statement above or some if condition.

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

public class MainListActivity extends ListActivity {

protected String[] mAndroidNames;


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


@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;
    Resources resources = getResources();
    mAndroidNames = resources.getStringArray(R.array.android_names);

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

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

this is the code

1 Answer

YUCHIH YU
YUCHIH YU
6,364 Points

You should move the "return true" to the very last line of that method. That'll solve the problem. But the real problem is that you are on the wrong method, the second half of your code should be at onCreate() instead of onCreateOptionMenu().