我有一个简单的问题: 在第一个活动中,我将变量另存为int数:
valsat2= findViewById(R.id.valsatu);
sharedpreferences = getSharedPreferences(valsat, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
int n = Integer.parseInt(valsat2.getText().toString());
editor.putInt(valsat, n);
editor.commit();
在第二个活动中,我想使用此int编号,但是如果使用此代码,它将返回给我0:
public static final int valsat = 0 ;
TextView sat = (TextView) findViewById(R.id.valoresaturazione);
sharedpreferences = getSharedPreferences("valsat", Context.MODE_PRIVATE);
int valsatn = sharedpreferences.getInt("valsat", 0);
sat.setText(Integer.toString(valsatn));
我该如何解决?谢谢!
答案 0 :(得分:0)
将值保存到 me@mycomputer:~$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
==> The Xcode Command Line Tools will be installed.
Press RETURN to continue or any other key to abort
==> Searching online for the Command Line Tools
==> /usr/bin/sudo /usr/bin/touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
Password:
时,请使用以下方法:
SharedPreferences
因此,您将变量editor.putInt(valsat, n);
用作该值的键,但是它的值是多少?是valsat
吗?
如果是,那么您应该在其他活动中获得正确的值,但我想不是。
因此更改为:
"valsat"
您还为editor.putInt("valsat", n);
文件名使用了相同的变量,但是在其他活动中,再次使用了字符串SharedPreferences
。
这是两件事。
编辑
不要使用2个共享首选项文件。使用一个名字说“ valsat”的东西:
"valsat"
答案 1 :(得分:0)
您必须将值保存到“ valsat”字符串标签。像这样更改此部分-
valsat2= findViewById(R.id.valsatu);
sharedpreferences = getSharedPreferences(valsat,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
int n = Integer.parseInt(valsat2.getText().toString());
editor.putInt("valsat", n);
editor.commit();
答案 2 :(得分:0)
您包含的错误消息很清楚,您的代码正在尝试在SharedPreferencesImpl.getInt()处将Integer转换为String。如果使用putString(),则需要先使用getString()检索String,然后才能将其转换为所需的类型。您不能使用putString()再使用getInt()。
Caused by:
java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Integer at
android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:272)
在第一个代码段中,您存储了两个字符串。
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences("valori", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("valsat", n);
editor.putString("valbic", m);
editor.commit();
在第二段代码中,您使用了getInt():
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences("valori", Context.MODE_PRIVATE);
int valbicn = sharedpreferences.getInt("valbic",0);
int valsatn = sharedpreferences.getInt("valsat",0);
您应该将String存储为一个int(很可能是您想要的),或者将它存储并检索为String,然后将其强制转换为int。前一种方法如下:
SharedPreferences sharedpreferences;
sharedpreferences = getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt("valsat", Integer.parseInt(n));
editor.putInt("valbic", Integer.parseInt(m));
editor.apply();
...
SharedPreferences sharedpreferences;
sharedpreferences = getDefaultSharedPreferences(getActivity());
int valbicn = sharedpreferences.getInt("valbic", 0);
int valsatn = sharedpreferences.getInt("valsat", 0);
sat.setText(String.valueOf(valsatn));
bic.setText(String.valueOf(valbicn));
另一种方法是将String存储为String,然后将其转换为int:
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("valsat", n);
editor.putString("valbic", m);
editor.apply();
...
SharedPreferences sharedpreferences;
sharedpreferences = getDefaultSharedPreferences(getActivity());
int valbicn = Integer.parseInt(sharedpreferences.getString("valbic", ""));
int valsatn = Integer.parseInt(sharedpreferences.getString("valsat", ""));
sat.setText(String.valueOf(valsatn));
bic.setText(String.valueOf(valbicn));
您的原始代码和错误消息:
// activity 1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNew = (Button) findViewById(R.id.button1);
valsat2= findViewById(R.id.valsatu);
valbic2= findViewById(R.id.valbicu);
sharedpreferences = getSharedPreferences("valori", Context.MODE_PRIVATE);
//sharedpreferences = getSharedPreferences(valbic, Context.MODE_PRIVATE);
btnNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n= valsat2.getText().toString();
String m= valbic2.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("valsat", n);
editor.putString("valbic", m);
editor.commit();
Intent intent = new Intent(MainActivity.this, Main3Activity.class);
startActivity(intent);
}
});
}
// activity 2
public class Main5Activity extends AppCompatActivity {
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
TextView sat = (TextView) findViewById(R.id.valoresaturazione);
TextView bic = (TextView) findViewById(R.id.valorebicarbonati);
sharedpreferences = getSharedPreferences("valori", Context.MODE_PRIVATE);
int valbicn = sharedpreferences.getInt("valbic",0);
int valsatn = sharedpreferences.getInt("valsat",0);
sat.setText(String.valueOf(valsatn));
bic.setText(String.valueOf(valbicn));
}
}
以下例外:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.osas, PID: 32694
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.osas/com.example.osas.Main5Activity}:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at
android.app.ActivityThread.-wrap11(Unknown Source:0) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at
android.os.Handler.dispatchMessage(Handler.java:105) at
android.os.Looper.loop(Looper.java:164) at
android.app.ActivityThread.main(ActivityThread.java:6541) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at
android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:272) at
com.example.osas.Main5Activity.onCreate(Main5Activity.java:64) at
android.app.Activity.performCreate(Activity.java:6975) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
您可以在此处找到有关SharedPreferences的其他信息,包括使用getDefaultSharedPreferences():Why cannot save INT to SharedPreferences?