Java 未找到共享首选项
我正在尝试加载SharedReferences,但始终找不到共享首选项,我正在尝试从另一个类更新它,但这也不起作用。我也在使用lst适配器创建列表视图。对下面的代码有什么想法吗?我正在获取列表视图的备份hello字符串Java 未找到共享首选项,java,android,debugging,sharedpreferences,Java,Android,Debugging,Sharedpreferences,我正在尝试加载SharedReferences,但始终找不到共享首选项,我正在尝试从另一个类更新它,但这也不起作用。我也在使用lst适配器创建列表视图。对下面的代码有什么想法吗?我正在获取列表视图的备份hello字符串 import android.app.ListActivity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; im
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class WorldMenu extends ListActivity{
SharedPreferences prefs = null;
String splitter;
String[] worldList;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
context = getBaseContext();
prefs = context.getSharedPreferences("worldString", 1);
splitter = "Create World," + prefs.getString("worldString", "hello");
worldList = splitter.split(",");
setListAdapter(new ArrayAdapter<String>(WorldMenu.this,
android.R.layout.simple_list_item_1, worldList));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
if(position == 0){
Intent openWorldNamer = new Intent("this works no need to check");
startActivity(openWorldNamer);
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
在您创建首选项之前,它们并不存在,因此您将始终看到
“hello”
下面是我用来设置值的一些代码:
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putBoolean("LicenseAccepted", true);
editor.commit();
请注意,这与您使用的命名文件不同,但概念是相同的:-)在哪里以及如何创建/设置存储在
worldString
中的值?直到它设置为不存在:-)我在另一个类中更新,但是当我回到另一个类时,同样的事情发生了。是啊,现在问题已经解决了,我在另一个类中的共享首选项不会更新它。
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putBoolean("LicenseAccepted", true);
editor.commit();