问题
I have a confusing problem. I have a MainActivity with 2 actions : Update and Logout. The problem is when I run the activity that extends ListActivity the action bar doesn't appear. Below I have 2 images with 2 different extend types in MainActivity
Extending ActionBarActivity example
public class MainActivity extends ActionBarActivity
By extends ListActivity the result is the same as in the picture below. Basically I want to make the main activity with a ListView and an action bar so that the user is able to update and logout using the action bar. But it seems it doesn't work and i need your help. I tried searching on the web i couldn't find anything that helped.
public class MainActivity extends ListActivity
Here you can see my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.florin.statusapp" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk android:minSdkVersion="11"
android:targetSdkVersion="21"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RegisterActivity"
android:label="@string/title_activity_register" >
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login" >
</activity>
<activity
android:name=".UpdateStatusActivity"
android:label="@string/title_activity_update_status" >
</activity>
</application>
</manifest>
My MainActivity.java
public class MainActivity extends ListActivity{
private List<ParseObject> mStatusObjects;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Enable Local Datastore.
Parse.initialize(this, "foo", "bar");
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
} else {
// show the login screen
Intent toLoginActivity = new Intent(MainActivity.this, LoginActivity.class);
startActivity(toLoginActivity);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_main, menu);
//return true;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id) {
case R.id.updateStatus:
// take user to update activity
Intent toMainActivityIntent = new Intent(MainActivity.this, UpdateStatusActivity.class);
startActivity(toMainActivityIntent);
break;
case R.id.LogoutUser:
//Log out user
ParseUser.logOut();
// take user to login activity
Intent toLoginActivityIntent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(toLoginActivityIntent);
break;
}
return super.onOptionsItemSelected(item);
}
and the menu_main.xml for the action bar:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.florin.statusapp.MainActivity">
<item android:id="@+id/updateStatus"
android:title="Update"
app:showAsAction="always" />
<item
android:id="@+id/LogoutUser"
android:title="Logout"
app:showAsAction="never"
/>
</menu>
回答1:
This should be related to your theme. Action bars are only supported on themes after holo.
http://developer.android.com/guide/topics/ui/actionbar.html#Adding
Your styles.xml probably has something like:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
You can change it to this to use the holo theme:
<style name="AppTheme" parent="android:Theme.Holo">
回答2:
As Tachyonflux said, on API 11 and higher, the action bar is included in all activities that use Theme.Holo or one of it's descendants
Try adding the following to your AndroidManifest.xml
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo">
Or another Theme or your choosing. Go to the link Tachyonflux has and look at the available options. There are various default options, but you can also create your own.
来源:https://stackoverflow.com/questions/28159021/actionbar-not-working-when-extending-listactivity