应用程序的快速概述:用户从列表中选择一个朋友,该朋友显示在微调器中。输入消息后,用户单击发送按钮。这就是应用程序崩溃的地方。
这是logcat的相关输出:
java.lang.ClassCastException:java.lang.String无法强制转换为com.parse.ParseUser at com.teamtreehouse.ribbit.SMSActivity.createMessage(SMSActivity.java:113) at com.teamtreehouse.ribbit.SMSActivity $ 1.onClick(SMSActivity.java:49)
这是相关代码:
ParseUser RecipientName = (ParseUser)friendsList.getSelectedItem();
// some other code
ParseObject message = new ParseObject(ParseConstants.CLASS_SMS);
message.put(ParseConstants.KEY_SENDER_NAME, RecipientName.getCurrentUser().getUsername());
//some other code
有什么想法吗?
完整性的完整代码:
public class SMSActivity extends Activity {
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected List<ParseUser> mFriends;
protected Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
}
public void onResume() {
super.onResume();
sendButton = (Button)findViewById(R.id.send_message_button);
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ParseObject message = createMessage();
if (message == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(SMSActivity.this);
builder.setMessage(R.string.error_sending_file).
setTitle(R.string.general_error).setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
send(message);
}
finish();
}
});
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
//get a list of all the friends of the user
ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
query.addAscendingOrder(ParseConstants.KEY_USERNAME);
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> friends, ParseException e) {
if (e == null) {
mFriends = friends;
String[] usernames = new String[mFriends.size()];
int i = 0;
for (ParseUser user : mFriends) {
usernames[i] = user.getUsername();
i++;
}
//attach the friendlist to the spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
SMSActivity.this,
android.R.layout.simple_spinner_dropdown_item,
usernames);
Spinner friendsList = (Spinner) findViewById(R.id.friends_list);
friendsList.setAdapter(adapter);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(SMSActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
protected ParseObject createMessage() {
EditText messageView = (EditText)findViewById(R.id.message_body);
String messageContent = messageView.getText().toString();
Spinner friendsList = (Spinner) findViewById(R.id.friends_list);
ParseUser RecipientName = (ParseUser)friendsList.getSelectedItem();
ParseObject message = new ParseObject(ParseConstants.CLASS_SMS);
message.put(ParseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser().getObjectId());
message.put(ParseConstants.KEY_SENDER_NAME, RecipientName.getCurrentUser().getUsername());
message.put(ParseConstants.KEY_RECIPIENT_IDS,
((ParseUser) friendsList.getSelectedItem()).getObjectId());
message.put(ParseConstants.KEY_SMS, messageContent);
return message;
}
protected void send(ParseObject message) {
message.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
//success
Toast.makeText(SMSActivity.this, R.string.success_message,Toast.LENGTH_SHORT);
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(SMSActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
答案 0 :(得分:1)
创建一个Map <String,ParseUser> map = new HashMap<String,ParseUser>()
...在这里你可以填写这张map.like
for (ParseUser user : mFriends) {
usernames[i] = user.getUsername();
map.put(user.getUsername(),user);
i++;
}
现在就像:
ParseUser RecipientName = map.get(friendsList.getSelectedItem());