我的ListView
自定义适配器存在一些问题。我不确定我的问题是xml文件,数据还是ListView
的实例化。我在设置适配器时收到NullPointerException
。适配器类:
public class InboxAdapter extends BaseAdapter {
private Context context;
private List<MailInbox> mails;
public InboxAdapter(Context context, List<MailInbox> mails)
{
this.context = context;
this.mails = mails;
}
@Override
public int getCount() {
return mails.size();
}
@Override
public Object getItem(int item) {
return mails.get(item);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
MailInbox entry = mails.get(position);
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.mail_list_row, null);
}
ImageView mailImg = (ImageView)convertView.findViewById(R.id.mail_thumb);
boolean read = entry.IsRead;
if(read)
{
//Set "read" image
mailImg.setImageResource(R.drawable.message);
} else
{
//Set "unread" image
mailImg.setImageResource(R.drawable.message);
}
TextView author = (TextView)convertView.findViewById(R.id.mail_text);
TextView body = (TextView)convertView.findViewById(R.id.mail_detail);
TextView date = (TextView)convertView.findViewById(R.id.mail_date);
author.setText(entry.AuthorName);
body.setText(entry.MessageBody);
date.setText(entry.CreatedAt);
return convertView;
}
}
我使用它的Activity
:
public class InboxActivity extends SherlockActivity
{
private ListView list;
private GetMessagesConnection mMessages;
public String kidID;
public InboxActivity() {
// TODO Auto-generated constructor stub
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox_layout);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
kidID = extras.getString("kidID");
}
new GetMails().execute();
}
private void setUpList()
{
list = (ListView)findViewById(R.id.mail_list);
ArrayList<MailInbox> mails = new ArrayList<MailInbox>();
for(int i = 0; i < mMessages.myMail.size(); i++)
{
MailInbox inbox = new MailInbox();
inbox.AuthorName = mMessages.myMail.get(i).AuthorName;
inbox.CreatedAt = mMessages.myMail.get(i).CreatedAt;
inbox.MessageBody = mMessages.myMail.get(i).MessageBody;
inbox.IsRead = mMessages.myMail.get(i).IsRead;
System.out.println("Author: " + inbox.AuthorName);
System.out.println("Time: " + inbox.CreatedAt);
System.out.println("Message: " + inbox.MessageBody);
System.out.println("Read: " + inbox.IsRead);
mails.add(inbox);
}
int length = mails.size();
System.out.println("Size: " + length);
InboxAdapter adapter = new InboxAdapter(this, mails);
// It crashes here with a Nullpointer Exception
list.setAdapter(adapter);
}
private class GetMails extends AsyncTask<Void, Void, Void> implements DialogInterface.OnCancelListener
{
private ProgressDialog dialog;
protected void onPreExecute()
{
dialog = ProgressDialog.show(InboxActivity.this, "", "Henter beskeder...", true);
}
protected Void doInBackground(Void... unused)
{
mMessages = new GetMessagesConnection();
mMessages.kidID = kidID;
mMessages.connection();
return null;
}
protected void onPostExecute(Void unused)
{
dialog.dismiss();
setUpList();
}
public void onCancel(DialogInterface dialog)
{
cancel(true);
dialog.dismiss();
}
}
}
* mail_list *:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mail_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2" >
</ListView>
* mail_list_row *:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="48dip"
android:background="@drawable/listselector"
>
<ImageView
android:id="@+id/mail_thumb"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/imgdesc"
/>
<TextView
android:id="@+id/mail_text"
android:layout_toRightOf="@+id/mail_thumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:layout_marginLeft="8dip"
android:layout_centerVertical="false"
android:singleLine="true"
android:ellipsize="end"
android:textStyle="bold"
android:textColor="@android:color/white"
/>
<TextView
android:id="@+id/mail_detail"
android:layout_toRightOf="@+id/mail_thumb"
android:layout_below="@+id/mail_text"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:textSize="12dip"
android:layout_marginLeft="8dip"
android:layout_centerVertical="false"
android:singleLine="true"
android:ellipsize="end"
android:textColor="@color/grey"
/>
<TextView
android:id="@+id/mail_date"
android:layout_toRightOf="@+id/mail_detail"
android:layout_below="@+id/mail_text"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:textSize="12dip"
android:singleLine="true"
android:ellipsize="end"
android:textColor="@color/grey"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
我不知道可能是null
。我一直试图解决这个问题几个小时。
错误日志:
05-14 19:55:45.525: E/AndroidRuntime(2509): FATAL EXCEPTION: main
05-14 19:55:45.525: E/AndroidRuntime(2509): java.lang.NullPointerException
05-14 19:55:45.525: E/AndroidRuntime(2509): at com.cim.daycare.InboxActivity.setUpList(InboxActivity.java:75)
05-14 19:55:45.525: E/AndroidRuntime(2509): at com.cim.daycare.InboxActivity.access$2(InboxActivity.java:36)
05-14 19:55:45.525: E/AndroidRuntime(2509): at com.cim.daycare.InboxActivity$GetMails.onPostExecute(InboxActivity.java:101)
05-14 19:55:45.525: E/AndroidRuntime(2509): at com.cim.daycare.InboxActivity$GetMails.onPostExecute(InboxActivity.java:1)
05-14 19:55:45.525: E/AndroidRuntime(2509): at android.os.AsyncTask.finish(AsyncTask.java:417)
05-14 19:55:45.525: E/AndroidRuntime(2509): at android.os.AsyncTask.access$300(AsyncTask.java:127)
05-14 19:55:45.525: E/AndroidRuntime(2509): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
05-14 19:55:45.525: E/AndroidRuntime(2509): at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 19:55:45.525: E/AndroidRuntime(2509): at android.os.Looper.loop(Looper.java:123)
05-14 19:55:45.525: E/AndroidRuntime(2509): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-14 19:55:45.525: E/AndroidRuntime(2509): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 19:55:45.525: E/AndroidRuntime(2509): at java.lang.reflect.Method.invoke(Method.java:507)
05-14 19:55:45.525: E/AndroidRuntime(2509): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-14 19:55:45.525: E/AndroidRuntime(2509): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-14 19:55:45.525: E/AndroidRuntime(2509): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
在活动InboxActivity
中,您将内容视图设置为R.layout.inbox_layout
(我在您的问题中看不到此文件),并搜索标识为{{1的ListView
}}。 R.id.mail_list
文件中存在ListView
id
。您确定mail_list.xml
的内容视图不是InboxActivity
:
R.layout.mail_list
此外:
请不要为//...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mail_list);
Bundle extras = getIntent().getExtras();
//...
声明构造函数,在InboxActivity
方法中初始化您想要的东西(这就是android世界中的工作原理)。
您在onCreate
extras
时处理案件了吗? (如果null
为extras
,则null
也将为kidID
,如果您不处理此问题,这可能会让您在null
课程遇到麻烦场景)。
答案 1 :(得分:1)
您可以将实例添加到列表中,例如list = (ListView)findViewById(R.id.mail_list);
,这意味着您正在检查该ID的当前布局,但您当前的布局设置为setContentView(R.layout.inbox_layout);
什么是inbox_layout?您还没有向我们展示XML?您的意思是将其设置为setContentView(R.layout.mail_list);
吗?
编辑:此外,您可能希望在自定义适配器中使用ViewHolder,以避免重复调用findViewByID(...)
。看一下本教程:http://xjaphx.wordpress.com/2011/06/16/viewholder-pattern-caching-view-efficiently/