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);
    }