Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.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 共享参考can';不显示数据整数_Java_Android_Json_Api_Sharedpreferences - Fatal编程技术网

Java 共享参考can';不显示数据整数

Java 共享参考can';不显示数据整数,java,android,json,api,sharedpreferences,Java,Android,Json,Api,Sharedpreferences,我尝试从sharedpref显示字符串类型并成功,但如果我尝试从sharedpref显示数据整型,则无法显示。我在下面显示我的代码 这是LoginAct.java中的一些代码 try { JSONObject jsonObject = new JSONObject(response.body().string()); if (jsonObject.getString("status").equals("true")) { Toast.makeText(c

我尝试从sharedpref显示字符串类型并成功,但如果我尝试从sharedpref显示数据整型,则无法显示。我在下面显示我的代码

这是LoginAct.java中的一些代码

try {
     JSONObject jsonObject = new JSONObject(response.body().string());

     if (jsonObject.getString("status").equals("true")) {

         Toast.makeText(context, "Login Success", Toast.LENGTH_SHORT).show();

         Integer no_id = jsonObject.getJSONObject("data").getInt("no_id");

         prefManager.saveSPInt(SharedPrefManager.SP_NO_ID, no_id);

         prefManager.saveSPBoolean(SharedPrefManager.SP_LOGGED, true);

         startActivity(new Intent(context, MainAct.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
         finish();}
}
public class SharedPrefManager {
    public static final String SP_APPS_APP = "spAppsApp";

    public static final String SP_NO_ID = "spNoID";

    public static final String SP_LOGGED = "spLogged";

    SharedPreferences sp;
    SharedPreferences.Editor spEditor;

    public SharedPrefManager(Context context){
        sp = context.getSharedPreferences(SP_APPS_APP, Context.MODE_PRIVATE);
        spEditor = sp.edit();
    }

    public void saveSPInt(String keySP, Integer value){
        spEditor.putInt(keySP, value);
        spEditor.commit();
    }

    public Integer getSpNoId() {
    return sp.getInt(SP_NO_ID, -1);
    }
}
这是我的SharedRef.java

try {
     JSONObject jsonObject = new JSONObject(response.body().string());

     if (jsonObject.getString("status").equals("true")) {

         Toast.makeText(context, "Login Success", Toast.LENGTH_SHORT).show();

         Integer no_id = jsonObject.getJSONObject("data").getInt("no_id");

         prefManager.saveSPInt(SharedPrefManager.SP_NO_ID, no_id);

         prefManager.saveSPBoolean(SharedPrefManager.SP_LOGGED, true);

         startActivity(new Intent(context, MainAct.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
         finish();}
}
public class SharedPrefManager {
    public static final String SP_APPS_APP = "spAppsApp";

    public static final String SP_NO_ID = "spNoID";

    public static final String SP_LOGGED = "spLogged";

    SharedPreferences sp;
    SharedPreferences.Editor spEditor;

    public SharedPrefManager(Context context){
        sp = context.getSharedPreferences(SP_APPS_APP, Context.MODE_PRIVATE);
        spEditor = sp.edit();
    }

    public void saveSPInt(String keySP, Integer value){
        spEditor.putInt(keySP, value);
        spEditor.commit();
    }

    public Integer getSpNoId() {
    return sp.getInt(SP_NO_ID, -1);
    }
}
这是MainAct.java中的一些代码,它不显示任何ID

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

                prefManager = new SharedPrefManager(this);

                tNoID = findViewById(R.id.txtNoID);
                tNoID.setText(prefManager.getSpNoID());
}

我仍然在工作,仍然困惑于解决它,我希望你能帮助我的情况下它。谢谢你们的帮助。

我想你们是说它要崩溃了。如果是这样,问题是不能使用任意整数值调用
setText()
。首先需要将其转换为
字符串
;e、 例如,
tNoID.setText(String.valueOf(prefManager.getSpNoID())
。我添加您的示例,并得到如下错误:java.lang.ClassCastException:java.lang.String不能转换为java.lang.Integer这与我给出的示例无关。但是,如果您以前将首选项保存为
字符串
,现在尝试将其检索为
int
,则可能会发生这种情况。如果您仍在测试,您可以在设备设置中选择应用程序存储页面上的“清除数据”选项,完全清除
SharedReferences
,然后重新开始。如果不是这样,我需要查看完整的堆栈跟踪才能确定问题所在。