为什么将 OnClickListener 分配给 AutoCompleteTextView 会给出 NullPointerException?

Why is assigning an OnClickListener to AutoCompleteTextView giving a NullPointerException?

每当我将 android:onClick 属性分配给 AutoCompleteTextView 时,我的应用程序会在加载 activity 时立即崩溃。我在没有分配 onClick 方法的情况下测试了 AutoCompleteTextView,它似乎工作正常,将 onClick 分配给其他视图(包括 EditText、Spinners、TextViews、Buttons 和 ImageButtons)也是如此。我已经设法将错误缩小到具体到第 24 行 (setContentView(R.layout.activity_main))。任何人都可以阐明这一点吗?

MainActivity.java

    package com.example.matt.testapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {
    public AutoCompleteTextView actv;
    public String[] continents = {"North America",
            "South America",
            "Antarctica",
            "Europe",
            "Africa",
            "Asia",
            "Australia"
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actv = (AutoCompleteTextView) findViewById(R.id.actv);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, continents);
        actv.setAdapter(adapter);
    }

    public void actvClick (View view) {}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent" >

    <AutoCompleteTextView
        android:id="@+id/actv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="actvClick" >
    </AutoCompleteTextView>
</LinearLayout>

Logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matt.testapp/com.example.matt.testapp.MainActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class AutoCompleteTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class AutoCompleteTextView
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class AutoCompleteTextView
Caused by: java.lang.NullPointerException: Attempt to write to field 'android.view.View$OnClickListener android.widget.AutoCompleteTextView$PassThroughClickListener.mWrapped' on a null object reference
at android.widget.AutoCompleteTextView$PassThroughClickListener.-set0(Unknown Source:0)
at android.widget.AutoCompleteTextView.setOnClickListener(AutoCompleteTextView.java:307)
at android.view.View.<init>(View.java:5008)
at android.widget.TextView.<init>(TextView.java:824)
at android.widget.EditText.<init>(EditText.java:88)
at android.widget.AutoCompleteTextView.<init>(AutoCompleteTextView.java:220)
at android.widget.AutoCompleteTextView.<init>(AutoCompleteTextView.java:193)
at android.widget.AutoCompleteTextView.<init>(AutoCompleteTextView.java:172)
at android.support.v7.widget.AppCompatAutoCompleteTextView.<init>(AppCompatAutoCompleteTextView.java:68)
at android.support.v7.widget.AppCompatAutoCompleteTextView.<init>(AppCompatAutoCompleteTextView.java:64)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:130)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1026)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1083)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:772)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.matt.testapp.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

这似乎是 AutoCompleteTextView 中的错误。

会发生什么:

  1. 膨胀 XML 调用 AutoCompleteTextView 构造函数,它调用 View 构造函数。
  2. View 构造函数看到 onClick 属性并调用 setOnClickListener().
  3. AutoCompleteTextView 覆盖了 setOnClickListener() 写入内部 mPassThroughClickListener 字段。它只是在AutoCompleteTextView构造函数的末尾初始化,而这个初始化代码还没有运行

Reference

要解决此问题,请从 XML 中删除 onClick 属性并在代码中以编程方式调用 setOnClickListener()

我在 Android 问题跟踪器中提交了关于此的 bug