Yevgeni Yevgeni - 4 months ago 6
Java Question

App crashes when trying to implement a custom list Adapter ( NullPointerException )

error:

05-17 11:27:39.986: E/AndroidRuntime(5172): FATAL EXCEPTION: main 05-17 11:27:39.986: E/AndroidRuntime(5172): Process: com.example.yevsnap, PID: 5172
05-17 11:27:39.986: E/AndroidRuntime(5172): java.lang.NullPointerException
05-17 11:27:39.986: E/AndroidRuntime(5172): at com.example.yevsnap.MessageAdapter.getView(MessageAdapter.java:46)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.AbsListView.obtainView(AbsListView.java:2240)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.ListView.makeAndAddView(ListView.java:1790)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.ListView.fillDown(ListView.java:691)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.ListView.fillFromTop(ListView.java:752)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.ListView.layoutChildren(ListView.java:1630)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.AbsListView.onLayout(AbsListView.java:2091)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.View.layout(View.java:14817) 05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.View.layout(View.java:14817) 05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.View.layout(View.java:14817) 05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.View.layout(View.java:14817) 05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.View.layout(View.java:14817)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-17 11:27:39.986: E/AndroidRuntime(5172): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374) 05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.View.layout(View.java:14817)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.View.layout(View.java:14817) 05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.ViewGroup.layout(ViewGroup.java:4631)
-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.Choreographer.doFrame(Choreographer.java:544) 05-17 11:27:39.986: E/AndroidRuntime(5172): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.os.Handler.handleCallback(Handler.java:733)
-17 11:27:39.986: E/AndroidRuntime(5172): at android.os.Handler.dispatchMessage(Handler.java:95)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.os.Looper.loop(Looper.java:136)
05-17 11:27:39.986: E/AndroidRuntime(5172): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-17 11:27:39.986: E/AndroidRuntime(5172): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 11:27:39.986: E/AndroidRuntime(5172): at java.lang.reflect.Method.invoke(Method.java:515) 05-17 11:27:39.986: E/AndroidRuntime(5172): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-17 11:27:39.986: E/AndroidRuntime(5172): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-17 11:27:39.986: E/AndroidRuntime(5172): at dalvik.system.NativeStart.main(Native Method)


Im pretty sure the code is right:

main part of the presenting list fragment:

@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++;
}
//SIMPLE LIST WORKS:
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(), android.R.layout.simple_list_item_1, usernames);

//this crashes:
MessageAdapter adapter = new MessageAdapter(getListView()
.getContext(), mMessages);

setListAdapter(adapter);
}
}
});
}


And the custom arrayAdapter class. MessageAdapter.java:

package com.example.yevsnap;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.parse.ParseObject;

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

Answer

You've forgotten to store the holder to the view. A null is returned when attempting to retrieve the holder when convertView is given for recycling.

Add

convertView.setTag(holder);

to the block where you instantiate the holder.

Comments