new android activity not starting
hello guys I an trying to switch to a new activity from my main activity but I am getting 'the application has stopped unexpectedly. Please try again'
. I am not sure why.
here is my current activity:
public class EditActivity extends ListActivity {
TextView selection;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListContent));
}
private static String[] mListContent={"Fishing Reports", "Choose a Fishing Spot", "Prediction","Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3"};
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
switch (position){
case 0 : Toast.makeText(this, "You pressed item 1 !", Toast.LENGTH_LONG).show();
break;
case 1 :
Intent myIntent = new Intent(EditActivity.this, LocationActivity.class);
startActivity(myIntent);
break;
}
super.onListItemClick(l, v, position, id);
}
here is the activity I an calling
public class LocationActivity extends ListActivity {
//Your member variable declaration here
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
}
}
The new activity is declared in my manifest
<activity android:name=".LocationActivity"></activity>
here is the layout of my new activity:
the current activity works fine but when I click on second item on mListContent (case 1)
, it does not launc开发者_如何学Ch the second activity, I get the error 'the application has stopped unexpectedly. Please try again'
any ideas why?
Thanks a lot!!
When you use a ListActivity, you need to make sure that your layout has a ListView with an id of "@android:id/list" so the ListActivity can find it. If you don't, it will force close on startup.
Something like this:
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fastScrollEnabled="true" />
</LinearLayout>
BTW, if you're using Eclipse, here's a tip to help with debugging.
In the Eclipse menu, choose Window > Show View > Other..., expand the Android category, select LogCat, and click OK. This will open the LogCat view which captures all the log output from your emulator or device.
I like to set up a filter in LogCat so it's easier to find the app messages; click the little green plus symbol to create a new filter, and enter "AndroidRuntime" for the "Filter Name" and "by Log Tag" fields. This filter will show up as another tab on the LogCat view, you can filter the contents any time by switching to that tab.
精彩评论