Reputation: 375
I just want to create a simple go back(home)button but I got the following errors
e(12674): FATAL EXCEPTION: main
06-11 15:38:59.910: E/AndroidRuntime(12674): java.lang.IllegalArgumentException: Activity DevicesActivity does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> element in your manifest?)
06-11 15:38:59.910: E/AndroidRuntime(12674): at android.support.v4.app.NavUtils.navigateUpFromSameTask(NavUtils.java:74)
06-11 15:38:59.910: E/AndroidRuntime(12674): at com.example.wip.DevicesActivity.onOptionsItemSelected(DevicesActivity.java:252)
06-11 15:38:59.910: E/AndroidRuntime(12674): at com.actionbarsherlock.app.SherlockExpandableListActivity.onMenuItemSelected(SherlockExpandableListActivity.java:197)
06-11 15:38:59.910: E/AndroidRuntime(12674): at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:603)
My codes:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Manifest
<activity
android:name="com.project.project1.MainActivity"
android:label="Project1" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.project.project1.SecondActivity"
android:label="@string/title_activity_second"
android:parentActivityName="com.project.wip.MainActivity" >
</activity>
Also Can I do something like this? The code works. anything bad about the following code?
case android.R.id.home:
Intent intent = new Intent(secondActivity.this,
MainActivity.class);
startActivity(intent);
return true;
Upvotes: 1
Views: 772
Reputation: 23483
The problem with your code is the fact that you don't have the following code in your manifest:
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
Chances are you are using a device that isn't ICS, which is why it isn't working.
There is nothing wrong with the code you have at the bottom, but I would put it into a menu like so:
@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, menu);
...
case android.R.id.home:
Intent intent = new Intent(secondActivity.this,
MainActivity.class);
startActivity(intent);
....
return true;
}
This will lay the foundation for you to add buttons to go to other area's, as well as home.
Upvotes: 1