I have a rare problem in an Android project with Java. It happens that when I start my application, it tells me:
"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setChecked (boolean)' on a null object reference"
But, everything is correct, the layout, the ID exists, everything is correct, the weird thing is, that it is only solved when I modify the activity, I give an enter or a space and it already works, but when I run it it already gives the same error in another activity and I have to be fixing like that all the time, it's frustrating. What could be generating this?
Example code MainActivity:
protected void onCreate(Bundle paramBundle) {
setTheme(R.style.AppThemeDark);
super.onCreate(paramBundle);
setContentView(R.layout.activity_main);
Switch switch_ = (Switch) findViewById(R.id.theme_switch);
switch_.setChecked(this.isDark);
this.themeSwitch = switch_;
}
Resource:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:tools="http://schemas.android.com/tools"
android:id="@id/drawer_layout" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:id="@id/fragment_container" android:layout_width="fill_parent" android:layout_height="fill_parent" />
</RelativeLayout>
<com.google.android.material.navigation.NavigationView android:layout_gravity="start" android:id="@id/nav_view" android:fitsSystemWindows="true" android:layout_width="wrap_content" android:layout_height="fill_parent">
<androidx.core.widget.NestedScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:gravity="bottom" android:orientation="vertical" android:id="@id/nav_head_layout" android:background="@drawable/bg_gradient_nav_bg" android:layout_width="fill_parent" android:layout_height="130.0dip">
<ImageView android:layout_gravity="center" android:padding="5.0dip" android:layout_width="110.0dip" android:layout_height="80.0dip" android:src="@drawable/logo" />
<TextView android:textAppearance="@style/TextAppearance.AppCompat.Title" android:textSize="18.0sp" android:textColor="@color/white" android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10.0dip" android:text="@string/app_title" android:layout_marginStart="10.0dip" />
</LinearLayout>
<View android:background="@color/grey_transparent" android:layout_width="fill_parent" android:layout_height="1.0px" />
<androidx.recyclerview.widget.RecyclerView android:id="@id/recyclerView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:nestedScrollingEnabled="false" />
<View android:background="@color/grey_transparent" android:layout_width="fill_parent" android:layout_height="1.0px" />
<RelativeLayout android:gravity="center_vertical" android:padding="10.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="8.0dip">
<ImageView android:gravity="center_vertical" android:id="@id/image" android:layout_width="20.0dip" android:layout_height="20.0dip" android:layout_marginStart="15.0dip" app:srcCompat="@drawable/ic_brightness_3_black_24dp" />
<TextView android:textSize="12.0sp" android:textStyle="bold" android:textColor="@color/default_text" android:gravity="start" android:id="@id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/dark_mode" android:layout_marginStart="20.0dip" android:layout_toEndOf="@id/image" />
<Switch android:gravity="center_vertical" android:id="@id/theme_switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true"
/>
</RelativeLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>