问题描述
我使用的共享preferences检查应用程序使用在第一次或not.if的应用程序使用首次登记屏幕会显示,这MainActivity后会显示是否注册succussfully完成或使用后来。注册画面显示了当应用程序运行过程中出现的第一次,但问题是有没有注册。同时呼吁从MainActivity共享preferences在其插入值。而问题是RuntimeException。这是我在做什么为...
这是我的MainActivity.java
共享preferences SP;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
firtst_time_check();
的setContentView(R.layout.available_items);} .....
这是first_time_check()
私人布尔firtst_time_check(){
SP = preferenceManager.getDefaultShared preferences(本);
第一串= sp.getString(第一,NULL);
如果(第一== NULL){
意图I =新意图(getBaseContext(),MainActivity.class);
startActivity(ⅰ);
}
返回false;
}
这是从注册屏幕code,其中的例外是在这一行抛 MainActivity设置=新MainActivity();
JSONArray A = o.optJSONArray(状态);
的JSONObject物镜= a.optJSONObject(0);
字符串的uid = obj.optString(RegistrationID);
MainActivity设置=新MainActivity();
set.sp = preferenceManager.getDefaultShared preferences(getApplicationContext());
字符串值= set.sp.getString(RegistrationID,UID);
共享preferences.Editor编辑= set.sp.edit();
editor.putString(第一,值);
editor.commit();
这是显示的logcat
lang.RuntimeException:不能内螺纹已不叫尺蠖prepare创造处理器()
没有,需要使用 SP
从 MainActivity
按创建活动的对象的在注册屏幕
共享preferences 保存价值>,因为 preferenceManager.getDefaultShared preferences
也返回共享preferences
实例,我们可以使用用于保存和它的键值。变化:
MainActivity集=新MainActivity();
set.sp = preferenceManager.getDefaultShared preferences(getApplicationContext());
到
共享preferences SP = preferenceManager.getDefaultShared preferences(
getApplicationContext());
i am using sharedpreferences to check the app is using for the first time or not.if the app is using for first time registration screen will show , and after that MainActivity will show if registration is completed succussfully or used later . Registration screen is showing when app is runnig for first time but the issue is there in Registering. while calling the shared preferences from MainActivity to insert value in it. and the issue is RuntimeException .this is what i am doing for that...
this is my MainActivity.java
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
firtst_time_check();
setContentView(R.layout.available_items);.....}
this is first_time_check()
private boolean firtst_time_check(){
sp = PreferenceManager.getDefaultSharedPreferences(this);
String first = sp.getString("first", null);
if (first == null){
Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);
}
return false;
}
this is code from Registration screen where exception is throwing in this line MainActivity set = new MainActivity();
JSONArray a = o.optJSONArray("status");
JSONObject obj = a.optJSONObject(0);
String uid = obj.optString("RegistrationID");
MainActivity set = new MainActivity();
set.sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String value = set.sp.getString("RegistrationID", uid);
SharedPreferences.Editor editor = set.sp.edit();
editor.putString("first", value);
editor.commit();
this is what showing in logcat
lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
No, need to use sp
from MainActivity
by creating object of Activity for saving value in SharedPreferences
in Registration screen
because PreferenceManager.getDefaultSharedPreferences
also return SharedPreferences
instance which we can use for saving and key-value in it. Change :
MainActivity set = new MainActivity();
set.sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
to
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(
getApplicationContext());
这篇关于RuntimeException的试图从服务器中提取,使用AsyncTask的共享preferences存储值时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!