我有一個令人困惑的問題。我有一個MainActivity有兩個操作:Update和Logout。問題是當我運行的活動延伸ListActivity操作欄不出現。 下面我有2張圖片包含有2個不同的在MainActivity當擴展ListActivity時,ActionBar不工作
延伸類型擴展ActionBarActivity例如
public class MainActivity extends ActionBarActivity
通過延伸ListActivity的結果是相同的,如下面的圖片。基本上,我想使主要活動與的ListView和一個ction酒吧,使用戶能夠更新和註銷使用操作欄。但它似乎不起作用,我需要你的幫助。我試圖在網上搜索,我找不到任何幫助。
public class MainActivity extends ListActivity
在這裏你可以看到我的清單文件:
<?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>
我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);
}
和操作欄中menu_main.xml:
<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>
在'ListView'中使用'ActionBarActivity'。 – 2015-01-27 01:00:00
看起來你是在從Udemy的講座教程工作羅尼:) – Heisenberg 2015-06-07 17:48:52
檢查: http://stackoverflow.com/questions/18532346/adding-actionbar-to-listactivity – 2015-07-02 21:35:07