Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom adapter's getview throws NullPointerException when variables are not null

I am trying to set custom adapter to display messages to user using demo chat application on list view , but receiving errors in getView method of my custom list adapter .

  • I did double check on my steps and debugging to find out if holder is not null . But holder is not null as you can see in my debugger variable output .

Error is on line :

holder.message.setText("lol");

What could be the possible mistake in my getView() of custom adapter ? Thanks for any hint and help in advance .

My Custom adapter :

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ChattingAdapter extends BaseAdapter{
    private Context mContext;
    private ArrayList<ChatMessage> mMessages;


    public ChattingAdapter(Context context, ArrayList<ChatMessage> messages) {
        super();
        this.mContext = context;
        this.mMessages = messages;
    }
    @Override
    public int getCount() {
        return mMessages.size();
    }
    @Override
    public Object getItem(int position) {       
        return mMessages.get(position);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChatMessage message = (ChatMessage) this.getItem(position);

        ViewHolder holder; 
        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listener, parent, false);
            holder.message = (TextView) convertView.findViewById(R.id.text1);
            convertView.setTag(holder);
        }
        else
            holder = (ViewHolder) convertView.getTag();
        Log.wtf("message", message.getMessage());
        //holder.message.setText(message.getMessage());
        holder.message.setText("lol");
        LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();      

        //Check whether message is mine to show green background and align to right

        if(message.isMine())

        {
                holder.message.setBackgroundResource(R.drawable.speech_bubble_green);
                lp.gravity = Gravity.RIGHT;

        }
        //If not mine then it is from sender to show orange background and align to left

        else

        {

            holder.message.setBackgroundResource(R.drawable.speech_bubble_orange);          
            lp.gravity = Gravity.LEFT;

        }
            holder.message.setLayoutParams(lp);
            //holder.message.setTextColor(R.color.textColor);   

        return convertView;
    }
    private static class ViewHolder
    {
        TextView message;
    }

    @Override
    public long getItemId(int position) {
        //Unimplemented, because we aren't using Sqlite.
        return position;
    }

}

My debugger results for variables :

holder  ChattingAdapter$ViewHolder  (id=830019560368)   
this    ChattingAdapter  (id=830019493992)  
message ChatMessage  (id=830019492960)  
position    0   
convertView LinearLayout  (id=830019561504) 
parent  ListView  (id=830018556296)

My Xml layouts : listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/text1"
  android:background="@drawable/speech_bubble_green"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:singleLine="false"
  android:textStyle="bold" />

listener.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical"
  android:padding="10dip" 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

    <ListView
      android:id="@android:id/list"
      android:layout_width="wrap_content" 
      android:layout_height="0px" 
      android:layout_weight="1"
       android:transcriptMode="normal"
      android:stackFromBottom="true"
      android:fadingEdge="none" android:padding="4dp"
      android:fastScrollEnabled="true" android:smoothScrollbar="false"
      android:focusable="true"/>

      <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:gravity="center_vertical"
        android:padding="4dp">

         <EditText android:id="@+id/chat_input"
           android:layout_width="0dip"
           android:layout_height="fill_parent"
           android:layout_weight="1"
           android:maxLines="5"
           android:inputType="textShortMessage|textAutoCorrect|textMultiLine|textCapSentences"
           android:imeOptions="actionSend|flagNoExtractUi" 
           android:cursorVisible="true"
           android:hint="Enter message" />

         <Button android:id="@+id/chat_send_message"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="send" />

        </LinearLayout>


</LinearLayout>

My stack trace:

02-21 16:48:36.008: E/Trace(18696): error opening trace file: No such file or directory (2)
02-21 16:49:00.008: A/Service(18696): connected
02-21 16:52:07.508: A/message(18696): Hello
02-21 16:52:26.018: E/AndroidRuntime(18696): FATAL EXCEPTION: main
02-21 16:52:26.018: E/AndroidRuntime(18696): java.lang.NullPointerException
02-21 16:52:26.018: E/AndroidRuntime(18696):    at com.example.konnect.ChattingAdapter.getView(ChattingAdapter.java:49)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.AbsListView.obtainView(AbsListView.java:2273)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.ListView.makeAndAddView(ListView.java:1769)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.ListView.fillUp(ListView.java:706)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.ListView.layoutChildren(ListView.java:1626)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.AbsListView.onLayout(AbsListView.java:2108)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.View.layout(View.java:13754)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.ViewGroup.layout(ViewGroup.java:4364)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.View.layout(View.java:13754)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.ViewGroup.layout(ViewGroup.java:4364)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.View.layout(View.java:13754)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.ViewGroup.layout(ViewGroup.java:4364)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.View.layout(View.java:13754)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.ViewGroup.layout(ViewGroup.java:4364)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.View.layout(View.java:13754)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.ViewGroup.layout(ViewGroup.java:4364)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1868)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1689)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.Choreographer.doFrame(Choreographer.java:525)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.os.Handler.handleCallback(Handler.java:615)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.os.Looper.loop(Looper.java:137)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at android.app.ActivityThread.main(ActivityThread.java:4931)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at java.lang.reflect.Method.invokeNative(Native Method)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at java.lang.reflect.Method.invoke(Method.java:511)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
02-21 16:52:26.018: E/AndroidRuntime(18696):    at dalvik.system.NativeStart.main(Native Method)
02-21 16:52:31.008: E/Trace(18898): error opening trace file: No such file or directory (2)

Function calling custom adapter on adding new messages:

ArrayList<ChatMessage> messagex= new ArrayList<ChatMessage>();;
ChattingAdapter adaptex;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.listener);
messagex.add(new ChatMessage("Hello", false));
adaptex  = new ChattingAdapter(getApplicationContext(),messagex);
setListAdapter(adaptex);
Button send_button = (Button) findViewById(R.id.chat_send_message);
sender_message = (EditText) findViewById(R.id.chat_input);
send_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        msg = sender_message.getText().toString();
        if(!(msg.length()==0)){
            messagex.add(new ChatMessage(msg, true));
            //addNewMessage(new ChatMessage(msg, true));
            adaptex.notifyDataSetChanged();
            getListView().setSelection(messagex.size()-1);
               }
        }});

ChatMessage class :

public class ChatMessage {

    String message;
    /**
     * boolean to determine, who is sender of this message
     */
    boolean isMine;

    //  boolean isStatusMessage;

        /**
         * Constructor to make a Message object
         */
        public ChatMessage(String message, boolean isMine) {
            super();
            this.message = message;
            this.isMine = isMine;
        //  this.isStatusMessage = false;
        }
        /**
         * Constructor to make a status Message object
         * consider the parameters are swaped from default Message constructor,
         *  not a good approach but have to go with it.
         */
        public ChatMessage(boolean status, String message) {
            super();
            this.message = message;
            this.isMine = false;
            //this.isStatusMessage = status;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
        public boolean isMine() {
            return isMine;
        }
        public void setMine(boolean isMine) {
            this.isMine = isMine;
        }
        /**
        public boolean isStatusMessage() {
            return isStatusMessage;
        }
        public void setStatusMessage(boolean isStatusMessage) {
            this.isStatusMessage = isStatusMessage;
        }
        */
}
like image 925
Harshit Avatar asked Feb 21 '14 11:02

Harshit


2 Answers

You have

convertView = LayoutInflater.from(mContext).inflate(R.layout.listener, parent, false);

and then

holder.message = (TextView) convertView.findViewById(R.id.text1);

But you have textview with id text1 in listitem.xml not listener.xml which you inflate.

listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/text1"

Hence you initialization fails

holder.message = (TextView) convertView.findViewById(R.id.text1);

Hence NullPointerException @

holder.message.setText("lol");

Solution : Change to

convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem, parent, false);
like image 61
Raghunandan Avatar answered Oct 24 '22 04:10

Raghunandan


Change :

convertView = LayoutInflater.from(mContext).inflate(R.layout.listener, parent, false);

to :

convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem, parent, false);
like image 44
Aparupa Ghoshal Avatar answered Oct 24 '22 02:10

Aparupa Ghoshal