0

I am having some strange problem with my text view on selected. So basically I have a list view on top, a text box and button at bottom. The list view is populated with data retrieved from database perfectly with no errors. However, when I tried to type in something at the text view, I am getting some error messages:

11-15 19:14:28.622: D/dalvikvm(1686): GC_CONCURRENT freed 622K, 13% free 10976K/12551K, paused 2ms+3ms
11-15 19:14:32.747: D/dalvikvm(1686): GC_CONCURRENT freed 431K, 12% free 11080K/12551K, paused 107ms+3ms
11-15 19:14:35.059: D/dalvikvm(1686): GC_CONCURRENT freed 428K, 11% free 11283K/12615K, paused 29ms+6ms
11-15 19:14:35.075: W/SurfaceView(1686): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false
11-15 19:14:42.973: D/dalvikvm(1686): GC_CONCURRENT freed 712K, 14% free 11216K/12935K, paused 2ms+3ms
11-15 19:15:27.770: D/AndroidRuntime(1686): Shutting down VM
11-15 19:15:27.770: W/dalvikvm(1686): threadid=1: thread exiting with uncaught exception (group=0x40c7e1f8)
11-15 19:15:27.864: E/AndroidRuntime(1686): FATAL EXCEPTION: main
11-15 19:15:27.864: E/AndroidRuntime(1686): java.lang.NullPointerException
11-15 19:15:27.864: E/AndroidRuntime(1686):     at nyp.edu.eneighbourhood.EventChat$ListAdapter.getView(EventChat.java:112)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.AbsListView.obtainView(AbsListView.java:2201)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.ListView.makeAndAddView(ListView.java:1774)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.ListViewa.fillSpecific(ListView.java:1318)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.ListView.layoutChildren(ListView.java:1603)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.AbsListView.onLayout(AbsListView.java:2052)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1634)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1492)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1405)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1671)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.os.Looper.loop(Looper.java:137)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at android.app.ActivityThread.main(ActivityThread.java:4512)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at java.lang.reflect.Method.invokeNative(Native Method)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at java.lang.reflect.Method.invoke(Method.java:511)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
11-15 19:15:27.864: E/AndroidRuntime(1686):     at dalvik.system.NativeStart.main(Native Method)
11-15 19:15:40.668: I/Process(1686): Sending signal. PID: 1686 SIG: 9

And here are the codes where I populate my list view and as well as submit button onclick:

public class EventChat extends Fragment {
Context context;
View eventChat;

String userID, eventID;

private ListView listview;
public ArrayList<EventComment> _commentlist = new ArrayList<EventComment>();
TextView txtDisplayCommentBy, txtDisplayDateTime, txtDisplayCommentDesc,
        txtEventChat;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    eventChat = inflater.inflate(R.layout.event_chat, container, false);
    context = getActivity();

    listview = (ListView) eventChat.findViewById(R.id.listview);
    txtEventChat = (TextView) eventChat.findViewById(R.id.txtEventChat);

    Intent i = getActivity().getIntent();
    _commentlist = (ArrayList<EventComment>) i
            .getSerializableExtra("eventCommentObj");
    Event eventModel = (Event) i.getSerializableExtra("eventObj");

    userID = "Gab";
    eventID = eventModel.getEventID();

    listview.setAdapter(new ListAdapter(getActivity()));

    return eventChat;
}

public void onSubmitChatClicked(View view){
    Log.i("C","C");
    EventComment commentModel = new EventComment();
    String currentDate = EventDateTime.getCurrentDate();
    String currentTime = EventDateTime.getCurrentTime();
    String commentDesc = String.valueOf(txtEventChat.getText());

    commentModel.setCommentBy(userID);
    commentModel.setEventID(eventID);
    commentModel.setCommentDate(currentDate);
    commentModel.setCommentTime(currentTime);
    commentModel.setCommentDesc(commentDesc);
    new CreateCommentAsyncTask(context).execute(commentModel);

}

private class ListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    ViewHolder viewHolder;

    public ListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return _commentlist.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.eventchat_listview_row,
                    null);
            viewHolder = new ViewHolder();

            viewHolder.txt_dcommentBy = (TextView) convertView
                    .findViewById(R.id.txtDisplayCommentBy);

            viewHolder.txt_ddateTime = (TextView) convertView
                    .findViewById(R.id.txtDisplayDateTime);

            viewHolder.txt_dcommentDesc = (TextView) convertView
                    .findViewById(R.id.txtDisplayCommentDesc);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txt_dcommentBy.setText(_commentlist.get(position)
                .getCommentBy().trim());

        viewHolder.txt_ddateTime
                .setText(_commentlist.get(position).getCommentDate().trim()
                        + "  "
                        + _commentlist.get(position).getCommentTime()
                                .trim());

        viewHolder.txt_dcommentDesc.setText(_commentlist.get(position)
                .getCommentDesc().trim());

        return convertView;
    }
}

private class ViewHolder {
    TextView txt_dcommentBy;
    TextView txt_ddateTime;
    TextView txt_dcommentDesc;
}
}

It was working fine if I just view the list view without typing anything at the text box. The error will only occur after I selected on the text box. Any ideas?

Thanks in advance.

  • It's this line: viewHolder.txt_dcommentBy.setText(_commentlist.get(position) .getCommentBy().trim()); But there is nothing wrong with the population of list view. –  Nov 15 '14 at 11:23
  • Also, if I cancel listview.setAdapter(new ListAdapter(getActivity())); this from onCreate to test the DB insertion, it threw me an error message of my button could not find the onclick method. But it was there :( –  Nov 15 '14 at 11:24
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Simon Nov 15 '14 at 11:26
  • @Simon Would you mind to point out what's wrong here? Because there is nothing wrong with the population of list view. It just only showing this message after I selected at the textbox below. And also, I tried with the previous comment, and I pretty sure there is something wrong with the button. Just that I could not figure out –  Nov 15 '14 at 11:28
  • @Simon Any ideas? Not being rude or offensive but I think the thread you provided does not help. –  Nov 15 '14 at 11:36

1 Answers1

1

The first time you set it it's fine since the convertView == null and you just create a new one. But then when your convertView != null, you get it's tag and convert it to a ViewHolder.

By then it's probably null, since you never actually set the tag.

Instead add this into the first if block:

convertView.setTag(viewHolder);

Furthermore the ViewHolder pattern, is usually a static inner class of a custom adapter:

private class ListAdapter extends BaseAdapter {
   private static class ViewHolder {
    TextView txt_dcommentBy;
    TextView txt_ddateTime;
    TextView txt_dcommentDesc;
   }
   ...
}
Simas
  • 43,548
  • 10
  • 88
  • 116