给出nullpointer异常Android的回调方法

时间:2013-12-10 12:40:00

标签: java android callback autocompletetextview

我正在尝试实例化autocompleteTextview,并且我有一个填充联系人列表的适配器。用户选择联系人后,我将手机号码传递给回拨方法。

这是我的回调界面。

public interface OnTaskCompleted {
        void taskCompleted(String errorMessage);
}

以下是我实例化自定义autocompleteTextview的方法。

myListener = new OnTaskCompleted() {

            @Override
            public void taskCompleted(String errorMessage) {


            }
        };
        SingleSelectContactmToField = new SingleSelectContact(getActivity().getApplicationContext(), myListener);

这是我的班级SingleSelectContact。

 public class SingleSelectContact extends MultiAutoCompleteTextView implements OnItemClickListener {
    private OnTaskCompleted mListener; 
    public SingleSelectContact(Context context, OnTaskCompleted listener) {
            super(context);
            init(context);
            mListener = listener;
        }
        /* Constructor */
        public SingleSelectContact(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }
        /* Constructor */
        public SingleSelectContact(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
            init(context);
        }
        /* set listeners for item click and text change */
        public void init(Context context){
            setOnItemClickListener(this);
            addTextChangedListener(textWather);
        } 


            public void onItemClick(AdapterView<?> parent, View view, int position,    long id) {
    Map<String, String> currentContact = (Map<String, String>)parent.getItemAtPosition(position);   
    String contactName = currentContact.get("Name");
    String contactId = currentContact.get("Number");
    setText(contactName);
    setSelection(getText().length());
            mListener.taskCompleted(contactId ); /*I GET A NULL POINTER EXECLPTION HERE*/
}

我已经调试了,我可以看到在构造函数中,侦听器得到了正确的实例化。但在onItemClick中,mListener的值显示为null。 感谢帮助。 这是我得到的错误

E/AndroidRuntime(24202): java.lang.NullPointerException

E/AndroidRuntime(24202):    at com.citrus.citruspay.SingleSelectContact.onItemClick(SingleSelectContact.java:119)

E/AndroidRuntime(24202):    at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:902)

E/AndroidRuntime(24202):    at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)

E/AndroidRuntime(24202):    at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1192)

E/AndroidRuntime(24202):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)

E/AndroidRuntime(24202):    at android.widget.AbsListView.performItemClick(AbsListView.java:1128)

E/AndroidRuntime(24202):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2812)

E/AndroidRuntime(24202):    at android.widget.AbsListView$1.run(AbsListView.java:3571)

E/AndroidRuntime(24202):    at android.os.Handler.handleCallback(Handler.java:725)

E/AndroidRuntime(24202):    at android.os.Handler.dispatchMessage(Handler.java:92)

E/AndroidRuntime(24202):    at android.os.Looper.loop(Looper.java:153)

E/AndroidRuntime(24202):    at android.app.ActivityThread.main(ActivityThread.java:5299)

E/AndroidRuntime(24202):    at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(24202):    at java.lang.reflect.Method.invoke(Method.java:511)

E/AndroidRuntime(24202):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)

E/AndroidRuntime(24202):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)

E/AndroidRuntime(24202):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

尝试直接在SingleSelectContact定义中定义您的侦听器,如下所示。要创建接口实例,首先需要实现它。因此,您无法创建与您类似的接口实例,但您可以为其创建实现。

SingleSelectContact mToField = new SingleSelectContact(getActivity().getApplicationContext(), new OnTaskCompleted() {

    @Override
    public void taskCompleted(String errorMessage) {


    }
});