ClassCastException android Integer to Long

时间:2016-04-13 23:09:34

标签: java android sharedpreferences

我在这个小测试应用程序中使用了一个整数作为金钱因素,但我意识到长期更合适并且我更改了代码以便钱是长而不是int并且我也适当地更改了SharedPreferences,但是它当我使用int时,它不起作用。谢谢你的帮助!

public class Home extends AppCompatActivity { 

SharedPreferences pref;
SharedPreferences.Editor editor;
Intent intent;
TextView home_money_view;
long money; // this is the variable that is causing problems
int initial;

final long TEST = (long)-1;
int gold_pieces;
int gold_price = 50;
TimerTask timerTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    home_money_view = (TextView) findViewById(R.id.home_money_view)

    pref = getApplicationContext().getSharedPreferences("MY_PREFS", MODE_PRIVATE);
    editor = pref.edit();


    money = pref.getLong("temp_money",Long.MIN_VALUE); // get value
    if (money==Long.MIN_VALUE){
        money=0;
    }

    gold_pieces = pref.getInt("temp_gold",-1);
    if (gold_pieces==-1){
        gold_pieces=0;
    }

    initial = pref.getInt("initial",0);
    money+=initial;
    editor.putInt("initial",0);
    editor.commit();

    home_money_view = (TextView) findViewById(R.id.home_money_view);
    home_money_view.setText(money+"");
    editor.commit();
}

public void backToSplash(View view){
    intent = new Intent(Home.this,BusinessSelector.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}

public void goToSecondView(View view){

    long temp_money = money;
    editor.putLong("temp_money",temp_money); // set value
    int temp_gold = gold_pieces;
    editor.putInt("temp_gold",temp_gold);
    editor.commit();
    intent = new Intent(Home.this,SecondView.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}

public void goToOtherView(View view) {

    long temp_money = money;
    editor.putLong("temp_money",temp_money); // set value

    int temp_gold = gold_pieces;
    editor.putInt("temp_gold", temp_gold);
    editor.commit();
    intent = new Intent(Home.this, Next.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}


}

logcat的:

04-13 19:01:03.675 12896-12896/com.exampleryancocuzzo.ryan.markettycoon   E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exampleryancocuzzo.ryan.markettycoon/com.exampleryancocuzzo.ryan.markettycoon.Home}: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
                                                                                          at android.app.ActivityThread.access$600(ActivityThread.java:123)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                          at android.os.Looper.loop(Looper.java:137)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:4424)
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                                       Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
                                                                                          at android.app.SharedPreferencesImpl.getLong(SharedPreferencesImpl.java:228)
                                                                                          at com.exampleryancocuzzo.ryan.markettycoon.Home.onCreate(Home.java:56)
                                                                                          at android.app.Activity.performCreate(Activity.java:4466)
                                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
                                                                                          at android.app.ActivityThread.access$600(ActivityThread.java:123) 
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                          at android.os.Looper.loop(Looper.java:137) 
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:4424) 
                                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 

0 个答案:

没有答案