java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View...' solution

Obvious Null Pointer Exception

The following code, the address is correct, but the content is not correct

i want to capture nation

public void inputFocus() { 
nation.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
        if (hasFocus) { 
            // here is the processing content when getting focus 
            ImageView imageView = ( ImageView) findViewById(R.id.login_diver2); 
            imageView.setBackgroundResource(R.color.input_dvier_focus); 
        } else { 
            // here is the processing content when the focus is lost 
            ImageView imageView = (ImageView) findViewById(R.id.login_diver2) ; 
            imageView.setBackgroundResource(R.color.input_dvier); 
        } 
    } 
});

}

But in fact my layout is written like this

<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:hint="Mainland China (+86)"
        android:background="@null"
        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:hint="Please fill in the phone number"
        android:password="true"
        android:background="@null"
        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" />

My nation should correspond to login_diver1, but if it is reversed, it says it is empty, so be careful next time

 

Guess you like

Origin blog.csdn.net/weixin_52213234/article/details/128039623