我正在根据teamtreehouse.com上的教程开发一个聊天应用程序
在EditFriendsActivity.java文件中编写完所有代码后,我运行了应用程序&我注意到编辑好友活动中没有ActionBar。
相反,ActionBar出现在其他活动中!
这里可能出现什么问题?
EditFriendsActivity.java:
public class EditFriendsActivity extends ListActivity {
public static final String TAG = EditFriendsActivity.class.getSimpleName();
protected List<ParseUser> mUsers;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_friends);
getActionBar();
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
@Override
protected void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.orderByAscending(ParseConstants.KEY_USERNAME);
query.setLimit(1000);
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> users, ParseException e) {
if (e == null) {
// Success!
mUsers = users;
String[] usernames = new String[mUsers.size()];
int i = 0;
for (ParseUser user : mUsers) {
usernames[i] = user.getUsername();
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
EditFriendsActivity.this,
android.R.layout.simple_list_item_checked,
usernames);
setListAdapter(adapter);
addFriendCheckmarks();
} else {
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(EditFriendsActivity.this);
builder.setTitle(R.string.error_title);
builder.setMessage(e.getMessage());
builder.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (getListView().isItemChecked(position)){
// add friend
mFriendsRelation.add(mUsers.get(position));
} else {
// remove friend
mFriendsRelation.remove(mUsers.get(position));
}
mCurrentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e != null) {
Log.e(TAG, e.getMessage());
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
/// -- menu ---///
}
private void addFriendCheckmarks() {
mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> friends, ParseException e) {
if (e == null) {
// list returned - look for a match
for (int i = 0; i < mUsers.size(); i++) {
ParseUser user = mUsers.get(i);
for (ParseUser friend: friends) {
if (friend.getObjectId().equals(user.getObjectId()));
getListView().setItemChecked(i, true);
}
}
} else {
Log.e(TAG, e.getMessage());
}
}
});
}
}
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hammadnasir.ribbit" >
<uses-sdk android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<application
android:name=".RibbitApplication"
android:allowBackup="true"
android:debuggable="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".SignUpActivity"
android:label="@string/title_activity_sign_up"
android:parentActivityName=".LoginActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".EditFriendsActivity"
android:label="@string/title_activity_edit_friends"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hammadnasir.ribbit.MainActivity" />
</activity>
<activity
android:name=".ChooseFriendsActivity"
android:label="@string/title_activity_choose_friends"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hammadnasir.ribbit.MainActivity" />
</activity>
</application>
</manifest>
请帮我解决这个问题。
答案 0 :(得分:1)
在扩展AppCompatActivity
之后,您应该在ListView
中添加RecyclerView
(或layout.xml
},然后实现用于填充和操作相应适配器中的对象的代码
当你不延伸ActionBarActivity
或AppCompatActivity
时,你不应该得到一个ActionBar
答案 1 :(得分:0)
在OnCreate方法下的类文件中包含它:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
并导入:
import android.support.v7.widget.Toolbar;