为什么将 OnClickListener 分配给 AutoCompleteTextView 会给出 NullPointerException? [英] Why is assigning an OnClickListener to AutoCompleteTextView giving a NullPointerException?

查看:32
本文介绍了为什么将 OnClickListener 分配给 AutoCompleteTextView 会给出 NullPointerException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我将 android:onClick 属性分配给 AutoCompleteTextView 时,我的应用程序在加载活动时立即崩溃.我在没有分配 onClick 方法的情况下测试了 AutoCompleteTextView,它似乎工作正常,就像将 onClick 分配给其他视图(包括 EditText、Spinners、TextViews、Buttons 和 ImageButtons)一样.我已经设法将错误范围缩小到第 24 行(setContentView(R.layout.activity_main)).任何人都可以对此有所了解吗?

Whenever I assign an android:onClick attribute to an AutoCompleteTextView, my application immediately crashes upon loading the activity. I've tested AutoCompleteTextView without assigning an onClick method and it seems to work fine, as does assigning onClick to other views (including EditText, Spinners, TextViews, Buttons and ImageButtons). I've managed to narrow down the error to specifically to line 24 (setContentView(R.layout.activity_main)). Can anyone shed some light on this?

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 中的一个错误.

This looks like to be a bug in AutoCompleteTextView.

发生了什么:

  1. 膨胀 XML 调用 AutoCompleteTextView 构造函数,它调用 View 构造函数.
  2. View 构造函数看到 onClick 属性并调用 setOnClickListener().
  3. AutoCompleteTextView 已覆盖写入内部 mPassThroughClickListener 字段的 setOnClickListener().仅在AutoCompleteTextView构造函数的末尾初始化,此初始化代码尚未运行.
  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.

参考

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

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

我在 Android 问题跟踪器中提交了一个关于此问题的错误.

I filed a bug about this in Android issue tracker.

这篇关于为什么将 OnClickListener 分配给 AutoCompleteTextView 会给出 NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆