Theme is not set by pressing Switch. App doesn't crash

Lulu96

Our App will run with 2 Themes. We defined it in themes.xml with AppTheme.White and AppTheme.Black. We know that the error is somewhere in our MainActivity code. If we press the Darkmode Switch in our Settings-Pop-Up the switch will be triggered but the themes won't change. We tried it isolated in our TestApp and the problem isn't the color or layout management. We are pretty sure something went wrong in our setOnCheckedChangeListener method. Thank you for your help! FYI: The App doesn't crash.

This Error appears:

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
        at com.example.LulusApp.BaseActivity.onOptionsItemSelected(BaseActivity.java:203)
        at android.app.Activity.onMenuItemSelected(Activity.java:3543)
        at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:383)
        at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:228)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at androidx.appcompat.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:65)
        at androidx.appcompat.widget.Toolbar$1.onMenuItemClick(Toolbar.java:207)
        at androidx.appcompat.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:779)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)

This is our MainActivity.class which includes a switch on it's Pop-Up Window:

    private final static int THEME_WHITE = 1;
    private final static int THEME_BLACK = 2;

public void updateTheme() {
        if (Utility.getTheme(getApplicationContext()) <= THEME_WHITE) {
            setTheme(R.style.AppTheme_White);
        } else if (Utility.getTheme(getApplicationContext()) == THEME_BLACK) {
            setTheme(R.style.AppTheme_Black);
        }
    }
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int dot_menu_items = item.getItemId();

    if (dot_menu_items == R.id.action_about) {
        setFragment(aboutFragment);
        return true;
    } else if (dot_menu_items == R.id.action_settings) {
        dialogBuilder = new AlertDialog.Builder(this);
        View settingsView = getLayoutInflater().inflate(R.layout.settings_popup, null);
        settingsView.setClipToOutline(true);
        dialogBuilder.setView(settingsView);
        dialog = dialogBuilder.create();
        dialog.show();

        try {
            Switch darkmodeSwitch = (Switch) findViewById(R.id.darkmode_switch_popup);
            int getcurrentmode = Utility.getTheme(getApplicationContext());
            if (getcurrentmode == 2) {
                darkmodeSwitch.setChecked(true);
            }
            darkmodeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                    if (isChecked) {
                        Utility.setTheme(getApplicationContext(), 2);
                    } else {
                        Utility.setTheme(getApplicationContext(), 1);
                    }
                    recreateActivity();
                }

            });
        }catch (Exception e){
            e.printStackTrace();
        }

        return true;
    } else if (dot_menu_items == R.id.action_premium) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Here our settings_popup.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/colorPrimary"
    android:layout_gravity="center"
    tools:context=".BaseActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:background="#7845ff">

        <Switch
            android:id="@+id/darkmode_switch_popup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="50px"
            android:background="#032342"
            android:padding="50px"
            android:text="Darkmode"
            android:textColor="?attr/colorPrimary"
            android:textSize="18sp"
            tools:ignore="UseSwitchCompatOrMaterialXml" />

    </LinearLayout>
</FrameLayout>

Also we defined a Utility.java for our sharedPreferences to save the changed Settings:

public class Utility {

    public static void setTheme(Context context, int theme) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putInt(context.getString(R.string.prefs_theme_key), theme).apply();
    }

    public static int getTheme(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getInt(context.getString(R.string.prefs_theme_key), 1);
    }
}

Here also our themes.xml:

<resources>
    <style name="AppTheme.White" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/primaryColor_white</item>
        <item name="colorPrimaryDark">@color/primaryColorDark_white</item>
        <item name="colorAccent">@color/primaryAccent_white</item>
        <item name="backgroundColor">@color/primaryColorDark_white</item>
        <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
    </style>
    <style name="AppTheme.Black" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/primaryColor_black</item>
        <item name="colorPrimaryDark">@color/primaryColorDark_black</item>
        <item name="colorAccent">@color/primaryAccent_black</item>
        <item name="backgroundColor">@color/primaryColorDark_black</item>
        <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
    </style>

    <style name="WindowAnimationTransition">
        <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
        <item name="android:windowExitAnimation">@android:anim/fade_out</item>
    </style>
</resources>

Also we defined colors for our Themes in the colors.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>
<color name="primaryColor_white">#6002ee</color>
<color name="primaryColorDark_white">#FFFFFF</color>
<color name="primaryAccent_white">#76FF03</color>

<color name="primaryColor_black">#FFFFFF</color>
<color name="primaryColorDark_black">#6002ee</color>
<color name="primaryAccent_black">#121212</color>

</resources>

Our Theme defined in our manifest.xml:

android:theme="@style/AppTheme.White">
Shay Kin

You get this error because you access to the Switch from your activity but it is in the Dialog View for fix that you need just to change this line

Switch darkmodeSwitch = findViewById(R.id.darkmode_switch_popup);

by this:

Switch darkmodeSwitch = settingsView.findViewById(R.id.darkmode_switch_popup);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

app crash and doesn't display Toast message

From Dev

Hibernate: app doesn't close after crash

From Dev

App crashes in release mode, but doesn't crash when zombies enabled

From Dev

Why doesn't Node.js + express app crash on Error?

From Dev

App crashes in release mode, but doesn't crash when zombies enabled

From Dev

Dynamically fill QTreeView so that app doesn't crash

From Dev

Powershell theming doesn't work: Set-Theme not found

From Dev

Android app theme textViewStyle doesn't work on phone

From Dev

Pressing home button and going back into the app causes the app to crash

From Dev

Why does my app crash when an NSPanel is closed by pressing Escape?

From Dev

scanf doesn't return when pressing enter

From Dev

Jpanel doesn't react when pressing ActionListerner

From Dev

Key Pressing in ProcessingJS Doesn't Work

From Dev

Theme neptune doesn't apply

From Dev

Prestashop theme doesn't work

From Dev

Theme neptune doesn't apply

From Dev

App crash when android:theme is aplied to a specific button

From Dev

How can I find the cause of crash? My app doesn't appear in crashlog

From Dev

Using Holo-Light theme doesn't set the correct color to TextViews

From Dev

Inheriting AppCompat 22.1.1 Dialog colorAccent from app theme doesn't work

From Dev

Why doesn't my app's theme get updated in my main activity anymore?

From Dev

nodejs app doesn't recognise batch environment variable set before

From Dev

Why my app crash when I switch fragments fast?

From Dev

Switch MediaView doesn't work

From Dev

Bootstrap switch doesn´t works

From Dev

Bootstrap tab doesn't switch

From Dev

Custom theme doesn't load theme css file in Drupal 8

From Dev

Android app: Switch images based on theme from code

From Dev

Subprocess doesn't wait and makes PhantomJS crash

Related Related

  1. 1

    app crash and doesn't display Toast message

  2. 2

    Hibernate: app doesn't close after crash

  3. 3

    App crashes in release mode, but doesn't crash when zombies enabled

  4. 4

    Why doesn't Node.js + express app crash on Error?

  5. 5

    App crashes in release mode, but doesn't crash when zombies enabled

  6. 6

    Dynamically fill QTreeView so that app doesn't crash

  7. 7

    Powershell theming doesn't work: Set-Theme not found

  8. 8

    Android app theme textViewStyle doesn't work on phone

  9. 9

    Pressing home button and going back into the app causes the app to crash

  10. 10

    Why does my app crash when an NSPanel is closed by pressing Escape?

  11. 11

    scanf doesn't return when pressing enter

  12. 12

    Jpanel doesn't react when pressing ActionListerner

  13. 13

    Key Pressing in ProcessingJS Doesn't Work

  14. 14

    Theme neptune doesn't apply

  15. 15

    Prestashop theme doesn't work

  16. 16

    Theme neptune doesn't apply

  17. 17

    App crash when android:theme is aplied to a specific button

  18. 18

    How can I find the cause of crash? My app doesn't appear in crashlog

  19. 19

    Using Holo-Light theme doesn't set the correct color to TextViews

  20. 20

    Inheriting AppCompat 22.1.1 Dialog colorAccent from app theme doesn't work

  21. 21

    Why doesn't my app's theme get updated in my main activity anymore?

  22. 22

    nodejs app doesn't recognise batch environment variable set before

  23. 23

    Why my app crash when I switch fragments fast?

  24. 24

    Switch MediaView doesn't work

  25. 25

    Bootstrap switch doesn´t works

  26. 26

    Bootstrap tab doesn't switch

  27. 27

    Custom theme doesn't load theme css file in Drupal 8

  28. 28

    Android app: Switch images based on theme from code

  29. 29

    Subprocess doesn't wait and makes PhantomJS crash

HotTag

Archive