gpt4 book ai didi

java - 在android中存储和获取共享首选项的值

转载 作者:行者123 更新时间:2023-12-01 13:06:57 25 4
gpt4 key购买 nike

我想将值存储在共享首选项中,并从共享首选项中获取值最终在 ListView 中显示值。接下来,我想在长按特定值时从列表中删除值。删除共享首选项中更新的值状态后。我完成了这一操作,但问题是当关闭应用程序并再次打开时,我必须在 ListView 中显示共享首选项值。但它显示 NullPointerExeption,

实际上我的要求是1)在共享首选项中存储值并在每次单击时在 ListView 中显示值。仅允许五个值。并且该列表值也可以在 onresume() 方法中使用。2)当长按 ListView 的特定值时,它必须删除,并且更新的值也存储在共享首选项中。

我的代码

public class ListViewDemo1 extends Activity {
/**
* Called when the activity is first created.
*/
Button btn;
static int count;
private ListView list;
public static ArrayList<String> values = new ArrayList<String>();
ArrayList countList = new ArrayList();
private ArrayAdapter adapter;
SharedPreferences shared;
Editor editor;
private static ArrayList<String> sharedList;

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

list = (ListView) findViewById(R.id.list);
btn = (Button) findViewById(R.id.btn);
shared = this.getSharedPreferences("Myprefernces", Context.MODE_WORLD_WRITEABLE);
editor = shared.edit();
btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
sharedList = new ArrayList();
count++;
if (count == 1) {
values.add("one");
countList.add(count);
}
if (count == 2) {
values.add("two");
countList.add(count);
}
if (count == 3) {
values.add("three");
countList.add(count);
}
if (count == 4) {
values.add("four");
countList.add(count);

}
if (count == 5) {
values.add("five");
countList.add(count);
}
if (count > 5) {
--count;
Toast.makeText(getApplicationContext(), "" + count, 100).show();
}
//put values to sharedpreferences
editor.putInt("SIZE", values.size());
for (int i = 0; i < values.size(); i++) {
editor.putString("addr" + i, values.get(i));
}
editor.commit();
// getting values from sharedpreference
int size = shared.getInt("SIZE", 0);
for (int k = 0; k < size; k++) {
sharedList.add(shared.getString("addr" + k, ""));
}
adapter = new
ArrayAdapter(ListViewDemo1.this, android.R.layout.simple_list_item_1, sharedList);
list.setAdapter(adapter);
}
});
list.setOnItemLongClickListener(new OnItemLongClickListener() {

public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
--count;
values.remove(arg2);
sharedList.remove(arg2);
editor.clear();
editor.commit();
editor.putInt("SIZE", sharedList.size());
for (int i = 0; i < sharedList.size(); i++) {
editor.putString("addr" + i, sharedList.get(i));
}
editor.commit();
adapter.notifyDataSetChanged();


return true;
}
});

}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (sharedList.size() > 0) {
adapter = new
ArrayAdapter(ListViewDemo1.this, android.R.layout.simple_list_item_1, sharedList);
list.setAdapter(adapter);
}
}
}

日志猫

04-21 09:56:02.142: E/AndroidRuntime(1160): FATAL EXCEPTION: main
04-21 09:56:02.142: E/AndroidRuntime(1160): java.lang.RuntimeException: Unable to resume activity {com.views/com.views.ListViewDemo1}: java.lang.NullPointerException
04-21 09:56:02.142: E/AndroidRuntime(1160): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-21 09:56:02.142: E/AndroidRuntime(1160): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-21 09:56:02.142: E/AndroidRuntime(1160): at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 09:56:02.142: E/AndroidRuntime(1160): at android.os.Looper.loop(Looper.java:137)
04-21 09:56:02.142: E/AndroidRuntime(1160): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-21 09:56:02.142: E/AndroidRuntime(1160): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 09:56:02.142: E/AndroidRuntime(1160): at java.lang.reflect.Method.invoke(Method.java:511)
04-21 09:56:02.142: E/AndroidRuntime(1160): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 09:56:02.142: E/AndroidRuntime(1160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-21 09:56:02.142: E/AndroidRuntime(1160): at dalvik.system.NativeStart.main(Native Method)
04-21 09:56:02.142: E/AndroidRuntime(1160): Caused by: java.lang.NullPointerException
04-21 09:56:02.142: E/AndroidRuntime(1160): at com.views.ListViewDemo1.onResume(ListViewDemo1.java:114)
04-21 09:56:02.142: E/AndroidRuntime(1160): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)

最佳答案

仅在按钮的 onClick() 方法中实例化 sharedList。当您的 Activity 恢复且 sharedList 为 null 时,不会调用此方法。

您需要检查它是否不为空:

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(sharedList != null && sharedList.size()>0){
adapter=new ArrayAdapter(ListViewDemo1.this,android.R.layout.simple_list_item_1,sharedList);
list.setAdapter(adapter);
}
}

关于java - 在android中存储和获取共享首选项的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23191009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com