Why is assigning an OnClickListener to AutoCompleteTextView giving a NullPointerException?

前端 未结 1 536
花落未央
花落未央 2021-01-23 12:09

Whenever I assign an android:onClick attribute to an AutoCompleteTextView, my application immediately crashes upon loading the activity. I\'ve tested AutoCompleteTextView withou

1条回答
  •  囚心锁ツ
    2021-01-23 12:31

    This looks like to be a bug in AutoCompleteTextView.

    What happens:

    1. Inflating the XML invokes AutoCompleteTextView constructor, and it invokes View constructor.
    2. View constructor sees the onClick attribute and calls setOnClickListener().
    3. AutoCompleteTextView has overridden setOnClickListener() which writes to an internal mPassThroughClickListener field. It is only initialized at the end of AutoCompleteTextView constructor, and this init code has not been run yet.

    Reference

    To work around it, remove the onClick attrtibute from your XML and call setOnClickListener() programmatically in your code.

    I filed a bug about this in Android issue tracker.

    0 讨论(0)
提交回复
热议问题