Android Textwatcher java.lang.NullPointerException错误。无法初始化事件侦听器

时间:2014-04-10 19:30:41

标签: android events android-edittext listener

我尝试在我的活动中使用带有EditText的TextWatcher。我已经为听众编写了这个类,我的代码编译没有错误。

但是在运行时,当我尝试将侦听器添加到EditText时,它会崩溃。给出nullPointerException。我相信我已将错误缩小到这一行:

    fatherBox.addTextChangedListener(new EditTextWatcher());

我真的看不到代码中的任何错误,EditText确实存在。我甚至开始怀疑Android Studio是否存在问题。

在创建方法上:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_clutch);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    EditText        fatherBox = (EditText) findViewById(R.id.clutch_father_box);
    fatherBox.addTextChangedListener(new EditTextWatcher());

//end on create
}

侦听器对象:

private class EditTextWatcher implements TextWatcher {

    public void afterTextChanged(Editable s) {


    }

    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Context context = getApplicationContext();
        CharSequence text = "Hello toast!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }

}

活动代码段

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@id/clutch_father_box"
    android:layout_below="@id/clutch_temp_box"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignLeft="@id/clutch_temp_box" />

1 个答案:

答案 0 :(得分:0)

让它工作得很好。 成为您的EditText对象。您指定的那行中没有其他对象甚至可以 null。 我的java

public class ActivityMain extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText text = (EditText) findViewById(R.id.clutch_father_box);
        text.addTextChangedListener(new EditTextWatcher());
    }

    private class EditTextWatcher implements TextWatcher {

        public void afterTextChanged(Editable s) {
            // No implementation
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // No implementation
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Toast.makeText(ActivityMain.this, R.string.app_name, Toast.LENGTH_SHORT).show();
        }
    }
}

我的xml

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

    <EditText
        android:id="@+id/clutch_father_box"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="10"
        android:inputType="textPersonName" />

</FrameLayout>

您确定编辑文字字段位于活动的布局文件中吗?你确定它不在PlaceholderFragment的布局中吗?

仔细检查以确保EditText对象不为空。尝试在获取引用之后和注册侦听器

之前粘贴以下代码段
if (fatherBox == null) {
    Toast.makeText(this, "I really am null", Toast.LENGTH_SHORT).show();
}

修改

PlaceHolderFragment班级中,覆盖onCreateView并使用EditText字段。即。

public class PlaceHolderFragment extends Fragment {

    private EditText fatherBox;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_place_holder, null, false);

        fatherBox = (EditText) rootView.findViewById(R.id.clutch_father_box);
        fatherBox.addTextChangedListener(new EditTextWatcher());

        return rootview;
    }

    private class EditTextWatcher implements TextWatcher {

        public void afterTextChanged(Editable s) {
            // No Implementation
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
            // No Implementation
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Toast.makeText(getActivity(), "Hello Toast!", Toast.LENGTH_SHORT).show();
        }
    }
}