我有一个ListFragment,我想使用自定义布局。布局非常好,所有资源都在他们的位置。但我仍然得到Null Pointer Exception。
列表片段代码:
public class InboxFragment extends ListFragment {
protected List<ParseObject> mMessages;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_inbox,
container, false);
return rootView;
}
@Override
public void onResume() {
super.onResume();
getActivity().setProgressBarIndeterminateVisibility(true);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(ParseConstants.CLASS_MESSAGES);
query.whereEqualTo(ParseConstants.KEY_RECIPIENT_IDS, ParseUser.getCurrentUser().getObjectId());
query.addDescendingOrder(ParseConstants.KEY_CREATED_AT);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> messages, ParseException e) {
getActivity().setProgressBarIndeterminateVisibility(false);
if (e == null) {
// We found messages!
mMessages = messages;
String[] usernames = new String[mMessages.size()];
int i = 0;
for(ParseObject message : mMessages) {
usernames[i] = message.getString(ParseConstants.KEY_SENDER_NAME);
i++;
}
MessageAdapter adapter = new MessageAdapter(
getListView().getContext(),
mMessages);
setListAdapter(adapter);
}
}
});
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ParseObject message = mMessages.get(position);
String messageType = message.getString(ParseConstants.KEY_FILE_TYPE);
ParseFile file = message.getParseFile(ParseConstants.KEY_FILE);
Uri fileUri = Uri.parse(file.getUrl());
if (messageType.equals(ParseConstants.TYPE_IMAGE)) {
// view the image
Intent intent = new Intent(getActivity(), ViewImageActivity.class);
intent.setData(fileUri);
startActivity(intent);
}
else {
// view the video
Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
intent.setDataAndType(fileUri, "video/*");
startActivity(intent);
}
}
}
我与此片段一起使用的适配器:
public class MessageAdapter extends ArrayAdapter<ParseObject> {
protected Context mContext;
protected List<ParseObject> mMessages;
public MessageAdapter(Context context, List<ParseObject> messages) {
super(context, R.layout.message_item, messages);
mContext = context;
mMessages = messages;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.message_item, null);
holder = new ViewHolder();
holder.iconImageView = (ImageView)convertView.findViewById(R.id.messageIcon);
holder.nameLabel = (TextView)convertView.findViewById(R.id.senderLabel);
}
else {
holder = (ViewHolder)convertView.getTag();
}
ParseObject message = mMessages.get(position);
if (message.getString(ParseConstants.KEY_FILE_TYPE).equals(ParseConstants.TYPE_IMAGE)) {
holder.iconImageView.setImageResource(R.drawable.ic_action_picture);
}
else {
holder.iconImageView.setImageResource(R.drawable.ic_action_play_over_video);
}
holder.nameLabel.setText(message.getString(ParseConstants.KEY_SENDER_NAME));
return convertView;
}
private static class ViewHolder {
ImageView iconImageView;
TextView nameLabel;
}
}
的图片第45行是这一行:
holder.iconImageView.setImageResource(R.drawable.ic_action_picture);
以下是布局的XML文件:
<ImageView
android:id="@+id/messageIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/message_list_item_vertical_margin"
android:paddingBottom="@dimen/message_list_item_vertical_margin"
android:src="@drawable/ic_action_picture" />
<TextView
android:id="@+id/senderLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/messageIcon"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="@dimen/activity_horizontal_margin" />
</RelativeLayout>
我的可绘制文件夹中有图像,但我仍然收到此错误。任何人都可以建议,这个代码有什么问题。
答案 0 :(得分:0)
在getView()中缺少导致holder=null
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.message_item, null);
holder = new ViewHolder();
holder.iconImageView = (ImageView)convertView.findViewById(R.id.messageIcon);
holder.nameLabel = (TextView)convertView.findViewById(R.id.senderLabel);
convertView.setTag(holder) // this line
}
else {
holder = (ViewHolder)convertView.getTag();
}
按照@CommonsWare评论的所有说明进行操作