Reputation: 591
I have a broadcast receiver that has the intent filter: ACTION_POWER_CONNECTED. The receiver is supposed to start a service. The receiver works great when the device has not been restarted, but once I restart the device and plug the device in, the app force closes. In my receiver I'm referencing another activity's static variable. Why does the app force close like that?
"dataSaved" is a SharedPreferences variable in MainActivity
if (MainActivity.dataSaved.getBoolean("User", false)) {
Intent i = new Intent(context, BatteryService.class)
context.startService(i);
}
Upvotes: 0
Views: 45
Reputation: 131
You said the dataSaved
is a static SharedPreference
, it is not initialized, so getBoolean
throws a NullPointerException
. You initialized it when somewhere in the MainActivity
but after restart no MainActivity
to initialize it
Get the sharedprefernce in the reciever
SharedPreferences dataSaved = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
boolean isUser = dataSaved.getBoolean("User", false);
Upvotes: 1