I am getting this error. What is crashing it? What could be the solution?
I want to store the inputs from mainactivity in a shared preference to use it in another service.
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3547)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
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:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:169)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
at android.content.Context.obtainStyledAttributes(Context.java:753)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
at com.clock.cuckoo.MainActivity.<init>(MainActivity.java:16)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1251)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3535)
... 11 more
My main activity looks like this:
SwitchMaterial[] switches = {findViewById(R.id.onoff),findViewById(R.id.is24hrs),findViewById(R.id.tts_status),findViewById(R.id.Sleep_time)};
EditText[] editTexts = {findViewById(R.id.start_time),findViewById(R.id.end_time)};
static SharedPreferences storage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent serviceIntent = new Intent(MainActivity.this, BootService.class);
MainActivity.this.startService(serviceIntent);
String[] ids = {"ISON","IS24HR","ISTTSON","SLEEP"};
for (String idLocation : ids){
if (!storage.getBoolean(idLocation, false)){
storage.edit().putBoolean(idLocation, false).apply();
}
}
for (int i = 1; i <= Array.getLength(switches); i++){
int finalI = i;
switches[i].setOnCheckedChangeListener((buttonView, isChecked) -> storage.edit().putBoolean(ids[finalI],isChecked).apply());
}
switches[0].setOnCheckedChangeListener((buttonView, isChecked) -> {
storage.edit().putBoolean(ids[0],isChecked).apply();
Intent serviceIntent1 = new Intent(MainActivity.this, Service.class);
MainActivity.this.startService(serviceIntent1);
});
}
Solution
Change your code to
SwitchMaterial[] switches;
EditText[] editTexts;
static SharedPreferences storage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switches = {findViewById(R.id.onoff), findViewById(R.id.is24hrs), findViewById(R.id.tts_status), findViewById(R.id.Sleep_time)};
editTexts = {findViewById(R.id.start_time), findViewById(R.id.end_time)};
Intent serviceIntent = new Intent(MainActivity.this, BootService.class);
MainActivity.this.startService(serviceIntent);
String[] ids = {"ISON", "IS24HR", "ISTTSON", "SLEEP"};
for (String idLocation : ids) {
if (!storage.getBoolean(idLocation, false)) {
storage.edit().putBoolean(idLocation, false).apply();
}
}
for (int i = 1; i <= Array.getLength(switches); i++) {
int finalI = i;
switches[i].setOnCheckedChangeListener((buttonView, isChecked) -> storage.edit().putBoolean(ids[finalI], isChecked).apply());
}
switches[0].setOnCheckedChangeListener((buttonView, isChecked) -> {
storage.edit().putBoolean(ids[0], isChecked).apply();
Intent serviceIntent1 = new Intent(MainActivity.this, Service.class);
MainActivity.this.startService(serviceIntent1);
});
}