Reputation: 199
I am using the SharedPreferences
to save and get my variables to my app when the app closes or starts.
I am using the same type of method/code for all my variables and it works, except for one variable.
I think maybe the problem might be that this variable also is programmed to reset to = 0 at a specific time each day. (I am using BroadcastReceiver for this).
Since this is the only variable that I reset at a specific time, and the same variable crashes when I try to save it with SharedPreferences, there might be something about those two things that make my app angry.
SharedPreferences settings2= getSharedPreferences(PREFS_NAME,0);
Specifically this line of code below seems to crash the app
dosesTaken = settings2.getInt("dosesTaken",dosesTaken);
This is the class for the "BroadcastReciver"
import ...
public class AlarmResetTaken extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MainActivity.dosesTaken = 0;
MainActivity.setDosage();
Activity2.takenDoseText.setText (Integer.toString(MainActivity.dosesTaken));
}
}
This is the method in MainActivity for the "BroadcastReciver"
private void setAlarmResetDose(long timeInMillis) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmResetTaken.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent,0);
alarmManager.setRepeating(AlarmManager.RTC, timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent);
}
And the method for setting the specific time to reset.
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timeDoseResetHour = hourOfDay;
timeDoseResetMin = minute;
timeDoseResetText.setText( String.format("%02d",hourOfDay) +" : " + String.format("%02d",minute));
Calendar calendar = Calendar.getInstance();
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
hourOfDay,
minute,
0
);
setAlarmResetDose(calendar.getTimeInMillis()); }
This is the error i get:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dex, PID: 28325
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dex/com.example.dex.MainActivity}: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:302)
at com.example.dex.MainActivity.onCreate(MainActivity.java:92)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
Upvotes: 2
Views: 90
Reputation: 233
Was the type of dosesTaken of type long? If it was, chances are it is saved as a long and now you're trying to retrieve it as an int, which results in the ClassCastException. Try and clear your shared preferences and rerun the app.
Upvotes: 0
Reputation: 14810
From the error that you have posted,
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
it seems that, the type of dosesTaken
is Long
and not int
. Thus, when you try to get an Integer with the name dosesTaken
, it will throw an error.
Thus, to solve the error, either you change
settings2.getInt("dosesTaken",dosesTaken);
to
settings2.getLong("dosesTaken",dosesTaken);
OR
change the type of dosesTaken
to int
.
Upvotes: 2
Reputation: 1445
Change this -:
settings2.getInt("dosesTaken",dosesTaken);
to-:
settings2.getLong("dosesTaken",dosesTaken);
Upvotes: 0