无法从 SharedPreferences 获取字符串?
unable to get strings from SharedPreferences?
我正在创建一个列表,使用共享 Preferences.I 来存储和保存值作为字符串 Preferences.I 想要存储事件的时间(一些事件)。
我的代码看起来像
package com.defcomdevs.invento16;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.util.Log;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
/**
* Created by midhun on 18/11/15.
*/
public class SetTimings {
// context to hold the calling activity
static Context c;
String[] event_times;
// declare file name and mode of access
public static String MY_PREFS = "MY_PREFS";
private SharedPreferences mySharedPreferences = new SharedPreferences() {
@Override
public Map<String, ?> getAll() {
return null;
}
@Nullable
@Override
public String getString(String key, String defValue) {
return null;
}
@Nullable
@Override
public Set<String> getStringSet(String key, Set<String> defValues) {
return null;
}
@Override
public int getInt(String key, int defValue) {
return 0;
}
@Override
public long getLong(String key, long defValue) {
return 0;
}
@Override
public float getFloat(String key, float defValue) {
return 0;
}
@Override
public boolean getBoolean(String key, boolean defValue) {
return false;
}
@Override
public boolean contains(String key) {
return false;
}
@Override
public Editor edit() {
return null;
}
@Override
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
}
@Override
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
}
};
int prefMode = c.MODE_PRIVATE;
public static String[] eventslist = new String[50];
public SetTimings(Context context, String[] strings) {
int i = 0;
Log.d("Midhun", strings[1]);
this.c = context;
eventslist = c.getResources().getStringArray(R.array.Items);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.apply();
Log.d("Midhun", mySharedPreferences.getString(eventslist[2], "null"));
}
public String[] getTimings(Context c1) {
eventslist = c1.getResources().getStringArray(R.array.Items);
for (int i = 0; i < eventslist.length; i++) {
event_times[i] = String.valueOf(mySharedPreferences.getString(eventslist[i], ""));
}
return event_times;
}
}
我这样调用 class 和构造函数:
setTimings= new SetTimings(this,this.getResources().getStringArray(R.array.Items));
并像这样访问字符串:
String[] time= setTimings.getTimings(this);
问题 当我 运行 我的应用程序崩溃时。
日志报告:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] com.defcomdevs.invento16.SetTimings.getTimings(android.content.Context)' on a null object reference
这表明错误在 getTimings()
方法中,我尝试在 SO 上提出一些问题,但没有解决。
请高亮我的error.And请给个solution.Thanks
编辑
我使用 setTimings
:
的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.mipmap.ic_launcher);
setSupportActionBar(toolbar);
getTheAccountName=new GetTheAccountName();
final Context context=this;
adapter = new MyAdapter(this, getdata());
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
mcollapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
mcollapsingToolbar.setTitle("INVENTO '16");
mcollapsingToolbar.setExpandedTitleColor(getResources().getColor(R.color.transparent));
mcollapsingToolbar.setCollapsedTitleTextColor(getResources().getColor(R.color.white));
fab_menu_titlescreen= (FloatingActionMenu) findViewById(R.id.menu_item);
fab_map = (FloatingActionButton) findViewById(R.id.map);
fab_register = (FloatingActionButton) findViewById(R.id.register);
fab_reminder = (FloatingActionButton) findViewById(R.id.reminder);
fab_refresh = (FloatingActionButton) findViewById(R.id.refresh);
String userName=getTheAccountName.getUsername(context);
String userId=getTheAccountName.getuserId(context);
//username= (TextView) findViewById(R.id.userNameText);
//username.setText(userName);
userid= (TextView) findViewById(R.id.userNameText);
userid.setText(userId);
Log.d("Midhun", "before");
String[] t= this.getResources().getStringArray(R.array.time);
//pass the time parameters to the class
setTimings= new SetTimings(this,t); //used setTimings
Log.d("Midhun","after");
在这里
public List<ListItems> getdata() {
List<ListItems> grid_data = new ArrayList<>();
int[] icons = {R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100};
String[] texts = getResources().getStringArray(R.array.Items);
String[] time= setTimings(context);
Log.d("Midhun",time[1]);
for (int i = 0; i < icons.length && i < texts.length; i++) {
ListItems current = new ListItems();
current.iconId = icons[i];
current.IconName = texts[i];
current.ItemTime = time[i];
grid_data.add(current);
}
return grid_data;
}
跟下面这行有关
editor.putString(eventslist[i++], strings[i++]);
你在这里做了两次 i++。
这意味着您没有引用正确的数组元素
运行 结果出界
您可能应该使用 for 循环来执行上述操作。
实际上,您可以 "save" 整个数组到 sharedpreferences
由于它在 getTimings() 中抛出 NullPointerException,您应该寻找一个为 null 的对象及其正在调用的方法。因此,候选人可能是:c1(上下文类型),eventList,mySharedPreference.
感谢 Mike M。
问题
1.First 是我的 Adapter 实例化中的 getdata() 是在实例化 setTimings 之前完成的,因此它在该方法中为空。
2.Second 我正在将字符串数组分配给未初始化的字符串数组 eventslist
。
3.As William Ku 指出我使用了 i++ 两次,因此着陆出界了。
我正在创建一个列表,使用共享 Preferences.I 来存储和保存值作为字符串 Preferences.I 想要存储事件的时间(一些事件)。
我的代码看起来像
package com.defcomdevs.invento16;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.util.Log;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
/**
* Created by midhun on 18/11/15.
*/
public class SetTimings {
// context to hold the calling activity
static Context c;
String[] event_times;
// declare file name and mode of access
public static String MY_PREFS = "MY_PREFS";
private SharedPreferences mySharedPreferences = new SharedPreferences() {
@Override
public Map<String, ?> getAll() {
return null;
}
@Nullable
@Override
public String getString(String key, String defValue) {
return null;
}
@Nullable
@Override
public Set<String> getStringSet(String key, Set<String> defValues) {
return null;
}
@Override
public int getInt(String key, int defValue) {
return 0;
}
@Override
public long getLong(String key, long defValue) {
return 0;
}
@Override
public float getFloat(String key, float defValue) {
return 0;
}
@Override
public boolean getBoolean(String key, boolean defValue) {
return false;
}
@Override
public boolean contains(String key) {
return false;
}
@Override
public Editor edit() {
return null;
}
@Override
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
}
@Override
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
}
};
int prefMode = c.MODE_PRIVATE;
public static String[] eventslist = new String[50];
public SetTimings(Context context, String[] strings) {
int i = 0;
Log.d("Midhun", strings[1]);
this.c = context;
eventslist = c.getResources().getStringArray(R.array.Items);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.putString(eventslist[i++], strings[i++]);
editor.apply();
Log.d("Midhun", mySharedPreferences.getString(eventslist[2], "null"));
}
public String[] getTimings(Context c1) {
eventslist = c1.getResources().getStringArray(R.array.Items);
for (int i = 0; i < eventslist.length; i++) {
event_times[i] = String.valueOf(mySharedPreferences.getString(eventslist[i], ""));
}
return event_times;
}
}
我这样调用 class 和构造函数:
setTimings= new SetTimings(this,this.getResources().getStringArray(R.array.Items));
并像这样访问字符串:
String[] time= setTimings.getTimings(this);
问题 当我 运行 我的应用程序崩溃时。
日志报告:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] com.defcomdevs.invento16.SetTimings.getTimings(android.content.Context)' on a null object reference
这表明错误在 getTimings()
方法中,我尝试在 SO 上提出一些问题,但没有解决。
请高亮我的error.And请给个solution.Thanks
编辑
我使用 setTimings
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.mipmap.ic_launcher);
setSupportActionBar(toolbar);
getTheAccountName=new GetTheAccountName();
final Context context=this;
adapter = new MyAdapter(this, getdata());
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
mcollapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
mcollapsingToolbar.setTitle("INVENTO '16");
mcollapsingToolbar.setExpandedTitleColor(getResources().getColor(R.color.transparent));
mcollapsingToolbar.setCollapsedTitleTextColor(getResources().getColor(R.color.white));
fab_menu_titlescreen= (FloatingActionMenu) findViewById(R.id.menu_item);
fab_map = (FloatingActionButton) findViewById(R.id.map);
fab_register = (FloatingActionButton) findViewById(R.id.register);
fab_reminder = (FloatingActionButton) findViewById(R.id.reminder);
fab_refresh = (FloatingActionButton) findViewById(R.id.refresh);
String userName=getTheAccountName.getUsername(context);
String userId=getTheAccountName.getuserId(context);
//username= (TextView) findViewById(R.id.userNameText);
//username.setText(userName);
userid= (TextView) findViewById(R.id.userNameText);
userid.setText(userId);
Log.d("Midhun", "before");
String[] t= this.getResources().getStringArray(R.array.time);
//pass the time parameters to the class
setTimings= new SetTimings(this,t); //used setTimings
Log.d("Midhun","after");
在这里
public List<ListItems> getdata() {
List<ListItems> grid_data = new ArrayList<>();
int[] icons = {R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100, R.drawable.hackbttn100};
String[] texts = getResources().getStringArray(R.array.Items);
String[] time= setTimings(context);
Log.d("Midhun",time[1]);
for (int i = 0; i < icons.length && i < texts.length; i++) {
ListItems current = new ListItems();
current.iconId = icons[i];
current.IconName = texts[i];
current.ItemTime = time[i];
grid_data.add(current);
}
return grid_data;
}
跟下面这行有关
editor.putString(eventslist[i++], strings[i++]);
你在这里做了两次 i++。 这意味着您没有引用正确的数组元素 运行 结果出界
您可能应该使用 for 循环来执行上述操作。 实际上,您可以 "save" 整个数组到 sharedpreferences
由于它在 getTimings() 中抛出 NullPointerException,您应该寻找一个为 null 的对象及其正在调用的方法。因此,候选人可能是:c1(上下文类型),eventList,mySharedPreference.
感谢 Mike M。
问题
1.First 是我的 Adapter 实例化中的 getdata() 是在实例化 setTimings 之前完成的,因此它在该方法中为空。
2.Second 我正在将字符串数组分配给未初始化的字符串数组 eventslist
。
3.As William Ku 指出我使用了 i++ 两次,因此着陆出界了。