0
我想實例化一個autocompleteTextview,並且我有一個適配器來填充聯繫人列表。一旦用戶選擇一個聯繫人,我將手機號碼傳遞給一個回叫方法。給出空指針異常的回調方法Android
這是我的回調接口。
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的值顯示爲空。 幫助表示讚賞。 以下是錯誤日誌,我得到
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)
請提供堆棧跟蹤? –
已編輯,請檢查。 – user1051505
嘗試在調用'init(context)'之前先分配監聽器? –