其中,我正在使用Parse.com集成和Sinch.com即时消息集成。 单击列表项后,将显示意外错误,特别是下面是日志消息。点击后,它会引导您进入即时消息传递活动,但是我收到了波纹管错误。
08-07 16:49:44.810: E/AndroidRuntime(11444): FATAL EXCEPTION: main
08-07 16:49:44.810: E/AndroidRuntime(11444): Process: com.dooba.beta, PID: 11444
08-07 16:49:44.810: E/AndroidRuntime(11444): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
08-07 16:49:44.810: E/AndroidRuntime(11444): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
08-07 16:49:44.810: E/AndroidRuntime(11444): at java.util.ArrayList.get(ArrayList.java:308)
08-07 16:49:44.810: E/AndroidRuntime(11444): at com.dooba.beta.MatchingActivity$3.done(MatchingActivity.java:90)
08-07 16:49:44.810: E/AndroidRuntime(11444): at com.parse.FindCallback.internalDone(FindCallback.java:45)
08-07 16:49:44.810: E/AndroidRuntime(11444): at com.parse.FindCallback.internalDone(FindCallback.java:1)
08-07 16:49:44.810: E/AndroidRuntime(11444): at com.parse.Parse$6$1.run(Parse.java:888)
08-07 16:49:44.810: E/AndroidRuntime(11444): at android.os.Handler.handleCallback(Handler.java:733)
08-07 16:49:44.810: E/AndroidRuntime(11444): at android.os.Handler.dispatchMessage(Handler.java:95)
08-07 16:49:44.810: E/AndroidRuntime(11444): at android.os.Looper.loop(Looper.java:136)
08-07 16:49:44.810: E/AndroidRuntime(11444): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-07 16:49:44.810: E/AndroidRuntime(11444): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 16:49:44.810: E/AndroidRuntime(11444): at java.lang.reflect.Method.invoke(Method.java:515)
08-07 16:49:44.810: E/AndroidRuntime(11444): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-07 16:49:44.810: E/AndroidRuntime(11444): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-07 16:49:44.810: E/AndroidRuntime(11444): at dalvik.system.NativeStart.main(Native Method)
以下是活动代码
public class MatchingActivity extends Activity {
private String currentUserId;
private ArrayAdapter<String> namesArrayAdapter;
private ArrayList<String> names;
private ListView usersListView;
private Button logoutButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.matching);
logoutButton = (Button) findViewById(R.id.logoutButton);
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ParseUser.logOut();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
}
});
setConversationsList();
}
private void setConversationsList() {
currentUserId = ParseUser.getCurrentUser().getObjectId();
names = new ArrayList<String>();
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereNotEqualTo("name", currentUserId);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> userList, ParseException e) {
if (e == null) {
for (int i=0; i<userList.size(); i++) {
names.add(userList.get(i).getUsername().toString());
}
usersListView = (ListView)findViewById(R.id.usersListView);
namesArrayAdapter =
new ArrayAdapter<String>(getApplicationContext(),
R.layout.user_list_item, names);
usersListView.setAdapter(namesArrayAdapter);
usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
openConversation(names, i);
}
});
} else {
Toast.makeText(getApplicationContext(),
"Error loading user list",
Toast.LENGTH_LONG).show();
}
}
});
}
public void openConversation(ArrayList<String> names, int pos) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("name", names.get(pos));
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> user, ParseException e) {
if (e == null) {
Intent intent = new Intent(getApplicationContext(), MessagingActivity.class);
intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),
"Error finding that user",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
以下是即时消息传递活动
public class MessagingActivity extends Activity implements ServiceConnection, MessageClientListener {
private String recipientId;
private Button sendButton;
private EditText messageBodyField;
private String messageBody;
private MessageService.MessageServiceInterface messageService;
private MessageAdapter messageAdapter;
private ListView messagesList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messaging);
doBind();
messagesList = (ListView) findViewById(R.id.listMessages);
messageAdapter = new MessageAdapter(this);
messagesList.setAdapter(messageAdapter);
Intent intent = getIntent();
recipientId = intent.getStringExtra("RECIPIENT_ID");
messageBodyField = (EditText) findViewById(R.id.messageBodyField);
sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
}
});
}
private void sendMessage() {
messageBody = messageBodyField.getText().toString();
if (messageBody.isEmpty()) {
Toast.makeText(this, "Please enter a message", Toast.LENGTH_LONG).show();
return;
}
messageService.sendMessage(recipientId, messageBody);
messageBodyField.setText("");
}
private void doBind() {
Intent serviceIntent = new Intent(this, MessageService.class);
bindService(serviceIntent, this, BIND_AUTO_CREATE);
}
@Override
public void onDestroy() {
unbindService(this);
super.onDestroy();
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//Define the messaging service and add a listener
messageService = (MessageService.MessageServiceInterface) iBinder;
messageService.addMessageClientListener(this);
if (!messageService.isSinchClientStarted()) {
Toast.makeText(this, "The message client did not start."
,Toast.LENGTH_LONG).show();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
messageService = null;
}
@Override
public void onMessageDelivered(MessageClient client, MessageDeliveryInfo deliveryInfo) {
//Intentionally left blank
}
@Override
public void onMessageFailed(MessageClient client, Message message,
MessageFailureInfo failureInfo) {
//Notify the user if message fails to send
Toast.makeText(this, "Message failed to send.", Toast.LENGTH_LONG).show();
}
@Override
public void onIncomingMessage(MessageClient client, Message message) {
messageAdapter.addMessage(message, MessageAdapter.DIRECTION_INCOMING);
}
@Override
public void onMessageSent(MessageClient client, Message message, String recipientId) {
messageAdapter.addMessage(message, MessageAdapter.DIRECTION_OUTGOING);
}
@Override
public void onShouldSendPushData(MessageClient client, Message message, List<PushPair> pushPairs) {
//Intentionally left blank
}
}
我的另一个问题是它在列表中返回OjectID,我更喜欢它返回User类的“name”,“headline”和“age”列。
如果您需要其他信息,请与我们联系。 提前谢谢。
答案 0 :(得分:2)
根据猫日志,我们可以确定问题的根源,MatchingActivity.done
中的第90行。
08-07 16:49:44.810: E/AndroidRuntime(11444): at com.dooba.beta.MatchingActivity$3.done(MatchingActivity.java:90)
你的问题在于这个方法:
public void openConversation(ArrayList<String> names, int pos) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("name", names.get(pos));
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> user, ParseException e) {
if (e == null) {
Intent intent = new Intent(getApplicationContext(), MessagingActivity.class);
//Below is line 90:
intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),
"Error finding that user",
Toast.LENGTH_SHORT).show();
}
}
});
}
源是IndexOutOfBoundsException,访问不存在的索引。在第90行,我们得到了以下电话:
intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
我们只在一个地方访问此行中的索引,此处为:
user.get(0).getObjectId()
因此,我们可以假设列表user
的大小为0,当访问列表中的索引时,请确保用户列表足够大。像这样:
if(!user.isEmpty()){
//the List user contains a user
Intent intent = new Intent(getApplicationContext(), MessagingActivity.class);
intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
startActivity(intent);
} else {
//the List user is empty do what you wish.
}
答案 1 :(得分:0)
你能检查一下userList的大小吗?在调试器中?这是在您的matchingActivity类
中我的猜测是它是一个空列表,你正在访问空列表的第一个元素