gpt4 book ai didi

android - 使用共享首选项时遇到空点异常

转载 作者:行者123 更新时间:2023-11-30 03:27:13 25 4
gpt4 key购买 nike

我有一个 Activity 在没有操作共享首选项的情况下工作。添加后,点击按钮出现空指针错误。

我认为我使用共享偏好的方式不正确。谁能帮忙指出是什么问题?我应该在使用前 new() 它吗?

public class BanknameActivity extends Activity {

String [] bank_name;
String selected_bank_config;
SharedPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bankname);
// Show the Up button in the action bar.
// setupActionBar();

bank_name = getResources().getStringArray(R.array.bankname);

ListView lv1 = (ListView) findViewById(R.id.bankname_listview);
lv1.setChoiceMode(1); // CHOICE_MODE_SINGLE

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, bank_name);
lv1.setAdapter(adapter);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
//selected_bank_config = "Citi";
prefs.edit().putString(selected_bank_config, "citi").commit();
return_to_config(view);
}
});

}

下面是我的部分代码:

    public void return_to_config(View view){
Intent intent = new Intent(this, ConfigActivity.class);
startActivity(intent);
this.finish();
}

最佳答案

**you need to initialize your shared Preferences. Without initialization it will always be null.**

yo need to do following stuff:

// mPrefs=getSharedPreferences("Preferences Name",mode);

mPrefs=getSharedPreferences("FBPref",0);
Editor edit=mPrefs.edit();
edit.putString("UserName", "");
edit.putString("UserId","");
edit.putString("EmailId","");
edit.commit();

这段代码运行良好。 我希望你会感激。

关于android - 使用共享首选项时遇到空点异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18044790/

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