java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.view.View...的解决方法

很明显的空指针异常

下面这段代码,地址对了,但是内容对不上

我想要捕获nation

public void inputFocus() {
nation.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            // 此处为得到焦点时的处理内容
            ImageView imageView = (ImageView) findViewById(R.id.login_diver2);
            imageView.setBackgroundResource(R.color.input_dvier_focus);
        } else {
            // 此处为失去焦点时的处理内容
            ImageView imageView = (ImageView) findViewById(R.id.login_diver2);
            imageView.setBackgroundResource(R.color.input_dvier);
        }
    }
});

}

但实际上我的layout内是这么写的

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:text="国家/地区"
        android:textColor="@color/loginText"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/log_nation"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="55dp"
        android:background="@null"
        android:hint="中国大陆(+86)"
        android:singleLine="true"
        android:textCursorDrawable="@drawable/edit_cursor_color"
        android:textSize="16sp" />
</LinearLayout>
<!--下划线-->
<ImageView
    android:id="@+id/login_diver1"
    android:layout_width="320dp"
    android:layout_height="1dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="17dp"
    android:background="@color/input_dvier" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:text="手机号"
        android:textColor="@color/loginText"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/log_phone"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="55dp"
        android:password="true"
        android:background="@null"
        android:hint="请填写手机号"
        android:singleLine="true"
        android:textColorHint="@color/textColorHint"
        android:textCursorDrawable="@drawable/edit_cursor_color"
        android:textSize="16sp"
        tools:ignore="Deprecated" />
</LinearLayout>
<!--下划线-->
<ImageView
    android:id="@+id/login_diver2"
    android:layout_width="320dp"
    android:layout_height="1dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="17dp"
    android:background="@color/input_dvier" />

我的nation对应的应该是login_diver1,但是写反了,它就说是空的,下次仔细一些

猜你喜欢

转载自blog.csdn.net/weixin_52213234/article/details/128039623