显示模拟器上运行的列表的相同代码无法在移动设备上运行(Motorola Milestone)。我正在使用相同的Android平台(2.1更新)。
private ArrayList<EmailModel> email_list;
private EmailAdapter email_adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
this.email_list = populate_list();
email_adapter = new EmailAdapter(this, email_list);
setListAdapter(email_adapter);
} catch (Exception e) {
Log.v("Inbox", "Adapting Problem: " + e.toString());
}
}
public class EmailAdapter extends BaseAdapter {
private ArrayList<EmailModel> elements;
private Context c;
public EmailAdapter(Context c, ArrayList<EmailModel> Emails) {
this.elements = Emails;
this.c = c;
}
public int getCount() {
return elements.size();
}
public Object getItem(int position) {
return elements.get(position);
}
public long getItemId(int id) {
return id;
}
public void Remove(int id) {
notifyDataSetChanged();
}
public void Add(EmailModel email) {
this.elements.add(email);
notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout rowLayout;
EmailModel email = elements.get(position);
if (convertView == null) {
rowLayout = (LinearLayout) LayoutInflater.from(c).inflate(R.layout.inbox_item, parent, false);
} else {
rowLayout = (LinearLayout) convertView;
}
TextView subject_textview = (TextView)rowLayout.findViewById(R.id.subject_textview);
subject_textview.setText(email.getSubject());
String body_hint = " - " + email.getBodyHint();
TextView bodyhint_textview = (TextView)rowLayout.findViewById(R.id.body_hint_textview);
bodyhint_textview.setText(body_hint);
String sender_name = get_sender_name(email.getSender());
TextView sender_name_textview = (TextView)rowLayout.findViewById(R.id.sender_textview);
sender_name_textview.setText(sender_name);
TextView date_time_textview = (TextView)rowLayout.findViewById(R.id.date_time_textview);
date_time_textview.setText(email.getTime());
return rowLayout;
}
private String get_sender_name(String from) {
String[] sender = from.split("<");
String sender_name;
try {
sender_name = sender[0];
} catch (Exception e) {
sender_name = sender[1];
}
return sender_name;
}}
07-16 13:15:21.190:VERBOSE / Inbox(429):改编问题:java.lang.ArrayIndexOutOfBoundsException
07-16 14:00:49.690:WARN / System.err(703):java.lang.ArrayIndexOutOfBoundsException 07-16 14:00:49.698:WARN / System.err(703):at com.anubis.mail.Inbox.populate_list(Inbox.java:237) 07-16 14:00:49.698:WARN / System.err(703):at com.anubis.mail.Inbox.onCreate(Inbox.java:69) 07-16 14:00:49.698:WARN / System.err(703):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 07-16 14:00:49.698:WARN / System.err(703):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 07-16 14:00:49.706:WARN / System.err(703):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 07-16 14:00:49.706:WARN / System.err(703):在android.app.ActivityThread.access $ 2200(ActivityThread.java:119) 07-16 14:00:49.706:WARN / System.err(703):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1863) 07-16 14:00:49.706:WARN / System.err(703):在android.os.Handler.dispatchMessage(Handler.java:99) 07-16 14:00:49.706:WARN / System.err(703):在android.os.Looper.loop(Looper.java:123) 07-16 14:00:49.706:WARN / System.err(703):在android.app.ActivityThread.main(ActivityThread.java:4363) 07-16 14:00:49.706:WARN / System.err(703):at java.lang.reflect.Method.invokeNative(Native Method) 07-16 14:00:49.706:WARN / System.err(703):at java.lang.reflect.Method.invoke(Method.java:521) 07-16 14:00:49.706:WARN / System.err(703):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860) 07-16 14:00:49.706:WARN / System.err(703):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 07-16 14:00:49.706:WARN / System.err(703):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
堆栈跟踪告诉我们您的方法Inbox.java
中populate_list()
的第237行发生了此异常。这不是适配器的问题。由于您未提供populate_list
方法的源代码,因此我们无法准确告诉您问题所在。
所以,请看Inbox.java
的第237行!
答案 1 :(得分:0)
我会将登录信息放入get_sender_name函数中。对我来说,sender_name = sender[0];
可能是错误但sender_name = sender[1];
没有错误,这对我没有多大意义。
此外,您应该使用Log.e
代替Log.v
来处理错误。 Log.v
用于传达不重要的信息,但只描述程序的运行方式。