当变量不为null时,自定义适配器的getview会抛出NullPointerException

时间:2014-02-21 11:35:33

标签: android android-adapter listadapter custom-adapter

我正在尝试设置自定义适配器,以便在列表视图上使用演示聊天应用程序向用户显示消息,但在我的自定义列表适配器的getView方法中接收错误。

  • 我仔细检查了我的步骤和调试,以确定持有人是否 不是空的。但是你可以在我的调试器中看到holder不是null 变量输出。

错误在线:

holder.message.setText("lol");

自定义适配器的getView()中可能出现的错误是什么?感谢任何提示和帮助提前。

我的自定义适配器:

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

}

我的调试器导致变量:

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

我的Xml布局: 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>

我的堆栈跟踪:

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)

在添加新消息时调用自定义适配器的函数:

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类:

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

2 个答案:

答案 0 :(得分:6)

你有

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

然后

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

但是您的listitem.xml而非listener.xml中的id text1的文字视图会膨胀。

listitem.xml

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

因此您初始化失败

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

因此NullPointerException @

holder.message.setText("lol");

解决方案:转到

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

答案 1 :(得分:1)

更改:

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

到:

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