Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ClassCastException android整数到长_Java_Android_Sharedpreferences - Fatal编程技术网

Java ClassCastException android整数到长

Java ClassCastException android整数到长,java,android,sharedpreferences,Java,Android,Sharedpreferences,我在这个小测试应用程序中使用了一个整数作为货币系数,但我意识到long更合适,我更改了代码,使货币是long而不是int,我也适当地更改了sharedReferences,但是它与我使用int时的情况不同。谢谢你的帮助 public class Home extends AppCompatActivity { SharedPreferences pref; SharedPreferences.Editor editor; Intent intent; TextView home_money_

我在这个小测试应用程序中使用了一个整数作为货币系数,但我意识到long更合适,我更改了代码,使货币是long而不是int,我也适当地更改了sharedReferences,但是它与我使用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);
}


}
日志:

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) 

另一方面,我将能够在几个小时内回复答案和评论,因为我将不在,但我会马上回来。在将应用程序首选项从int改为long后,您是否清除了应用程序首选项?当您将共享首选项存储为int时,共享首选项中可能仍然包含int。这会导致类强制转换异常,因为您试图长时间访问它。我添加了editor.clear,它可以工作只是为了将来参考,您可以通过转到app info>clear data来清除设备上某个应用的首选项。如果你不想编辑你的代码来清除它,这会很有用。哦,谢谢你,这真的很有用