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 trialHarsh Vardhan Bansal
Courses Plus Student 1,166 Pointsunreachable 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
Harsh Vardhan Bansal
Courses Plus Student 1,166 Pointsimport 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
6,364 PointsYou 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().
YUCHIH YU
6,364 PointsYUCHIH YU
6,364 PointsCan you post some snippet of yours. Probably you have some return statement above or some if condition.