gpt4 book ai didi

java - 索引越界异常 - 意外错误

转载 作者:行者123 更新时间:2023-12-02 05:34:07 25 4
gpt4 key购买 nike

除此之外,我正在处理 Parse.com 集成和 Sinch.com 即时消息集成。单击列表项后,会显示意外错误,特别是下面是日志消息。单击后,它应该将您引导至即时消息 Activity ,但是我收到了以下错误。

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)

下面是 Activity 代码

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();
}
}
});
}
}

下面是即时通讯 Activity

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”列。

如果您需要更多信息,请告诉我。提前致谢。

最佳答案

根据cat日志我们可以确定问题的根源,即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.
}

关于java - 索引越界异常 - 意外错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25188156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com