IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Switch item night mode


Sujet :

Android

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Novembre 2017
    Messages
    78
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Aisne (Picardie)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Novembre 2017
    Messages : 78
    Points : 36
    Points
    36
    Par défaut Switch item night mode
    Bonsoir,

    J'ai un petit soucis que je n'arrive pas à régler, si je fait mon NightMode sur le Switch qui à l'ID switchIconRightSettings, alors mon application plante quand je rentre sur ma page SettingsActivity, et j'ai ce message:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
            at com.debarflorian.imcapp.SettingsActivity.onCreate(SettingsActivity.java:87)
    Si je clique sur l'erreur, il m'ammène à ça:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
            switchNightMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
    Par contre j'ai essayé de rajouter un autre bouton Switch en dehors de ma ListView, et là par contre ça marche... mon application ne plante pas...

    Pouvez vous m'aider ?

    Cordialement.

    Voici mon code SettingsActivity

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
     
    public class SettingsActivity extends AppCompatActivity {
     
        private Toolbar toolbar;
        ListView listView;
        private Switch switchNightMode;
     
        @Override
        protected void onCreate (Bundle savedInstanceState) {
     
            if(AppCompatDelegate.getDefaultNightMode() ==  AppCompatDelegate.MODE_NIGHT_YES) {
                setTheme(R.style.AppThemeDark);
            } else {
                setTheme(R.style.AppTheme);
            }
     
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);
     
     
            // Action Bar
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
     
            List<Item> itemList = new ArrayList<>();
            itemList.add(new SectionItem("Préférences"));
            itemList.add(new SwitchItem(R.drawable.item_nightmode_icon, "", "Mode sombre"));
            itemList.add(new EntryItem(R.drawable.item_themecolor_icon, "Thème de l'application"));
            itemList.add(new SectionItem("Compte"));
            itemList.add(new EntryItem(R.drawable.item_profil_icon, "Profil"));
            itemList.add(new EntryItem(R.drawable.item_logout_icon, "Se déconnecter"));
     
            ListView listView = findViewById(R.id.listSettings);
            listView.setAdapter(new ItemAdapter(this, itemList));
            listView.setDivider(null);
     
            switchNightMode = (Switch) findViewById(R.id.switchIconRightSettings);
     
            if(AppCompatDelegate.getDefaultNightMode() ==  AppCompatDelegate.MODE_NIGHT_YES) {
                switchNightMode.setChecked(true);
            }
            switchNightMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if(isChecked) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                }
            });
     
        }
     
        public interface Item {
            public boolean isSwitch();
            public boolean isSection();
            public String getTitle();
        }
     
        public class SectionItem implements Item {
            private String title;
     
            public SectionItem (String title) {
                this.title = title;
            }
     
            public String getTitle () {
                return title;
            }
     
            @Override
            public boolean isSwitch() {
                return false;
            }
     
            @Override
            public boolean isSection() {
                return true;
            }
        }
     
        public class SwitchItem implements Item {
            private int switchIconleft;
            private String title;
            private String switchIconRight;
     
            public SwitchItem (int switchIconleft, String title, String switchIconRight) {
                this.switchIconleft = switchIconleft;
                this.title = title;
                this.switchIconRight = switchIconRight;
            }
     
            public int getSwitchIconleft () {
                return switchIconleft;
            }
     
            public String getTitle () {
                return title;
            }
     
            public String getSwitchIconRight () {
                return switchIconRight;
            }
     
            @Override
            public boolean isSwitch() {
                return true;
            }
     
            @Override
            public boolean isSection() {
                return false;
            }
        }
     
     
        public class EntryItem implements Item {
            private int iconleft;
            private String title;
     
            public EntryItem (int iconleft, String title) {
                this.iconleft = iconleft;
                this.title = title;
            }
     
            public int getIconleft () {
                return iconleft;
            }
     
            public String getTitle () {
                return title;
            }
     
            @Override
            public boolean isSwitch() {
                return false;
            }
     
            @Override
            public boolean isSection() {
                return false;
            }
     
     
            }
     
        public class ItemAdapter extends BaseAdapter {
     
            private Context context;
            private List<Item> itemList;
            private LayoutInflater inflater;
     
            public ItemAdapter(Context context, List<Item> itemList) {
                this.context = context;
                this.itemList = itemList;
                this.inflater = LayoutInflater.from(context);
            }
     
     
            @Override
            public int getCount () {
                return itemList.size();
            }
     
            @Override
            public Object getItem (int position) {
                return itemList.get(position);
            }
     
            @Override
            public long getItemId (int i) {
                return 0;
            }
     
            @Override
            public View getView (int i, View convertView, ViewGroup parent) {
     
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                if(itemList.get(i).isSection()) {
                    // if header
                    convertView = inflater.inflate(R.layout.header_item, null);
                    TextView itemHeaderTitle = convertView.findViewById(R.id.headerSectionTitle);
                    itemHeaderTitle.setText(((SectionItem) itemList.get(i)).getTitle());
                    convertView.setEnabled(false);
     
                } else if(itemList.get(i).isSwitch()) {
                    convertView = inflater.inflate(R.layout.switch_item, null);
     
                    // Switch Icon left
                    ImageView itemSwitchIconLeftView = convertView.findViewById(R.id.switchIconLeftSettings);
                    itemSwitchIconLeftView.setImageResource(((SwitchItem) itemList.get(i)).getSwitchIconleft());
     
                    TextView itemSwitchNameView = convertView.findViewById(R.id.switchNameSettings);
                    itemSwitchNameView.setText(((SwitchItem) itemList.get(i)).getSwitchIconRight());
     
                    Switch itemSwitchIconRightView = convertView.findViewById(R.id.switchIconRightSettings);
                    itemSwitchIconRightView.setText(((SwitchItem) itemList.get(i)).getTitle());
     
                } else {
                    // if item
                    convertView = inflater.inflate(R.layout.adapter_item, null);
     
                    // Icon left
                    ImageView itemIconView = convertView.findViewById(R.id.item_iconleft_settings);
                    itemIconView.setImageResource(((EntryItem) itemList.get(i)).getIconleft());
     
                    // Title
                    TextView itemNameView = convertView.findViewById(R.id.item_name_settings);
                    itemNameView.setText(((EntryItem) itemList.get(i)).getTitle());
     
                }
     
                return convertView;
            }
        }
     
     
     
        @Override
        public boolean onSupportNavigateUp () {
            onBackPressed();
            finish();
            return super.onSupportNavigateUp();
        }
     
     
        @Override
        public void finish() {
            super.finish();
            overridePendingTransition(R.anim.no_animation,  R.anim.no_animation);
        }

  2. #2
    Membre régulier Avatar de abdennour bouaicha
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2009
    Messages
    98
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Avril 2009
    Messages : 98
    Points : 112
    Points
    112
    Par défaut
    salut,
    le message d'erreur nullpointerExcption veut dire que l'objet est null ,soit il est déclaré sans constructeur soit sa classe contient des erreurs,
    autre remarque ,j'ai copié coller ton code dans mon éditeur et il manque une accolade } qui ferme cette classe , vérifie si ce n'est pas ça l'erreur?

Discussions similaires

  1. [PowerShell] tracer opération remove-item en mode vebose
    Par Boubou2020 dans le forum Scripts/Batch
    Réponses: 1
    Dernier message: 15/06/2018, 19h23
  2. Réponses: 4
    Dernier message: 03/06/2013, 12h10
  3. Réponses: 4
    Dernier message: 13/03/2009, 15h44
  4. sélectionner un item dans un listview en mode détail.
    Par grome dans le forum Windows Forms
    Réponses: 7
    Dernier message: 22/08/2007, 12h16
  5. [ethereal]promiscuous mode sur switch et hub
    Par Zetmurin dans le forum Développement
    Réponses: 8
    Dernier message: 08/09/2005, 16h25

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo