Bonjour à tous, j'ai fait un mode nuit sur mon application avec le code ci dessous :
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
public class Setting extends AppCompatActivity {
SharedPref sharedPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
sharedPref = new SharedPref(this);
if (sharedPref.loadNightModeState()){
setTheme(R.style.AppTheme);
} else{
setTheme(R.style.DarkTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting2);
Switch myswitch = findViewById(R.id.myswitch);
if (sharedPref.loadNightModeState()){
myswitch.setChecked(true);
}
Log.d("Setting", String.valueOf(myswitch));
myswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
sharedPref.setNightModeState(true);
} else{
sharedPref.setNightModeState(false);
}
restartapp();
}
});
ImageView home = findViewById(R.id.imagehome2);
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent deuxiemepage = new Intent(getApplicationContext(), MainActivity.class);
startActivity(deuxiemepage);
finish();
}
});
LinearLayout listes = findViewById(R.id.linearlayout);
listes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent b = new Intent(getApplicationContext(), MyListes.class);
startActivity(b);
}
});
}
public void restartapp () {
Intent i = new Intent(getApplicationContext(), Setting.class);
startActivity(i);
finish();
}
}
Ce mode nuit marche très bien à part sur android 10.0 ou il me met l'erreur suivante :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.Setting}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
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.example.myapplication.Setting.onCreate(Setting.java:38)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2020-06-24 12:59:33.767 2062-5365/system_process W/ActivityTaskManager: Force finishing activity com.example.myapplication/.Setting
2020-06-24 12:59:34.275 2062-2091/system_process W/ActivityTaskManager: Activity top resumed state loss timeout for ActivityRecord{3ec5270 u0 com.example.myapplication/.Setting t-1 f}
savez vous comment je pourrais faire pour que le switch ne sois pas égal à null?
problème avec switch sur android 10.0
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.