java Android SharedPreferences 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12905456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:43:52  来源:igfitidea点击:

Android SharedPreferences Example

javaandroidsharedpreferences

提问by EGHDK

I'm trying to learn SharedPreferences, but I'm getting an error.

我正在尝试学习 SharedPreferences,但出现错误。

My layout has one button that reeacts to the method doThis

我的布局有一个按钮可以对方法做出反应 doThis

This is my java:

这是我的java:

package com.example.sharedprefs;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    int i = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void doThis (View view){
        i++;
        SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
        SharedPreferences.Editor prefEditor = sharedPref.edit();
        prefEditor.putInt("userChoice",i);
        prefEditor.commit();
        int number = sharedPref.getInt("userChoice", 0);
        Toast.makeText(getApplicationContext(), number, Toast.LENGTH_LONG).show();
    }

}

Only thing I can pinpoint in logcat is 10-15 19:28:17.707: E/AndroidRuntime(16657): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1

我唯一可以在 logcat 中指出的是 10-15 19:28:17.707: E/AndroidRuntime(16657): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1

回答by ahodder

Your toast is incorrect. You are passing a number into the toast hoping that it will give a string, instead is thinks it should be looking up a string resource value. Try:

你的吐司不正确。您将一个数字传递到 toast 中,希望它会给出一个字符串,而不是认为它应该查找字符串资源值。尝试:

Toast.makeText(getContext(), number + "" , Toast.LENGTH_LONG).show();

Edit, other than that, your code is fine.

编辑,除此之外,您的代码很好。

回答by Dan Lee

You can't make a integer type to Toast message.

您不能为 Toast 消息创建整数类型。

you can only put String type to the message parameter to Toast.makeText method.

您只能将 String 类型添加到 Toast.makeText 方法的消息参数中。

as for solution, you can try these

至于解决方案,你可以试试这些

Toast.makeText(getApplicationContext(), Integer.toString(number), Toast.LENGTH_LONG).show();

Toast.makeText(getApplicationContext(), ""+number, Toast.LENGTH_LONG).show();

and yes, your sharedpreference usage is perfectly fine.

是的,您的 sharedpreference 用法非常好。

回答by Kashfa Khan

The problem here is you are using an integer value as the Toast string. You have to do the following.

这里的问题是您使用整数值作为 Toast 字符串。您必须执行以下操作。

String.valueOf(number);

or

或者

Integer.toString(number);

Your sharedpreferences part is ok. But for more info about SharedPreferences you can visit this post. Android SharedPreferences Example

您的共享首选项部分没问题。但是有关 SharedPreferences 的更多信息,您可以访问这篇文章。 Android SharedPreferences 示例