简体   繁体   English

检索共享首选项

[英]retrieving a shared preference

I have two activities A and B. 我有两个活动A和B.

B : is a setting activity that save all the setting in a shared preference. B:是一项设置活动,用于保存共享首选项中的所有设置。

A : is the main activity and I retrieve the shared preference that was saved in B activity 答:是主要活动,我检索B活动中保存的共享首选项

the problem is if the user use the application for the first time and launch the A activity its forced close. 问题是如果用户第一次使用该应用程序并启动其强制关闭的A活动。

I think because there is no shared preference saved yet... 我想因为没有保存共享偏好...

the A activity: A活动:

public class DawaaActivity extends ListActivity implements View.OnClickListener {

Button add;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences emportPref = getSharedPreferences("dawaaData",MODE_PRIVATE);
if (emportPref.getInt("exists", 0) == 1){
String dawaaList[] = {emportPref.getString("subject", null)};
setListAdapter(new ArrayAdapter<String>(DawaaActivity.this,
android.R.layout.simple_list_item_1,dawaaList));
}
initialaiz();
}

private void initialaiz() {

add = (Button) findViewById(R.id.button3);
add.setOnClickListener(this);
}

public void onClick(View v) {

Intent data = new Intent(DawaaActivity.this,SettingActivity.class);
startActivity(data);
}
}

the B activity: B活动:

public class SettingActivity extends Activity implements View.OnClickListener {
EditText et;
EditText et1;
Spinner list;
TimePicker startTime;
Button save;
Button cancel;
private SharedPreferences exportPref;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
initialaiz();
}
private void initialaiz() {

et = (EditText) findViewById(R.id.text);
et1 = (EditText) findViewById(R.id.text1);
list = (Spinner) findViewById(R.id.spinner);
startTime = (TimePicker) findViewById(R.id.timePicker);
save = (Button) findViewById(R.id.save);
cancel = (Button) findViewById(R.id.cancel);
save.setOnClickListener(this);
cancel.setOnClickListener(this);
}
public void onClick(View v) {

switch(v.getId()){
case R.id.save:
save();
Intent transferData = new Intent(SettingActivity.this,DawaaActivity.class);
startActivity(transferData);
break;
case R.id.cancel:
finish();
break;
}
}
private void save() {

exportPref = getSharedPreferences("dawaaData",MODE_PRIVATE);
SharedPreferences.Editor editor = exportPref.edit();
editor.putString("subject", et.getText().toString());
editor.putString("dosesC", et1.getText().toString());
editor.putString("doses", list.getSelectedItem().toString());
editor.putInt("hour", startTime.getCurrentHour());
editor.putInt("minute", startTime.getCurrentMinute());
editor.putInt("exists", 1);
editor.commit();
Toast.makeText(SettingActivity.this,"data has been saved" ,Toast.LENGTH_SHORT).show();
}

}

how I can solve the problem ??? 我怎么能解决问题???

EDIT: 编辑:

I add the ListView in my layout 我在我的布局中添加了ListView

and I edit the class like this: 我像这样编辑类:

public class DawaaActivity extends Activity implements View.OnClickListener {

ListView myList;
SharedPreferences emportPref = getSharedPreferences("dawaaData",MODE_PRIVATE);
String dawaaList[] = {emportPref.getString("subject", "no data yet")};
myList = (ListView) findViewById(R.id.ListView);
myList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, dawaaList));

and this is the logcat: 这是logcat:

08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.os.Looper.loop(Looper.java:123)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-28 14:56:41.112: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-28 14:56:41.112: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-28 14:56:41.112: E/AndroidRuntime(273):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.os.Looper.loop(Looper.java:123)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-28 14:59:54.272: E/AndroidRuntime(303):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-28 14:59:54.272: E/AndroidRuntime(303):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-28 14:59:54.272: E/AndroidRuntime(303):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
08-28 15:02:26.632: E/AndroidRuntime(331):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)

08-28 15:02:26.632: E/AndroidRuntime(331): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 08-28 15:02:26.632: E/AndroidRuntime(331): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 08-28 15:02:26.632: E/AndroidRuntime(331): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 08-28 15:02:26.632: E/AndroidRuntime(331): at android.os.Handler.dispatchMessage(Handler.java:99) 08-28 15:02:26.632: E/AndroidRuntime(331): at android.os.Looper.loop(Looper.java:123) 08-28 15:02:26.632: E/AndroidRuntime(331): at android.app.ActivityThread.main(ActivityThread.java:4627) 08-28 15:02:26.632: E/AndroidRuntime(331): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 08-28 15:02:26.632: E/AndroidRuntime(331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-28 15:02:26.632: E/AndroidRuntime(331): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146) 08-28 15:02 08-28 15:02:26.632:E / AndroidRuntime(331):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)08-28 15:02:26.632:E / AndroidRuntime(331):在android。 app.ActivityThread.access $ 2300(ActivityThread.java:125)08-28 15:02:26.632:E / AndroidRuntime(331):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)08-28 15 :02:26.632:E / AndroidRuntime(331):在android.os.Handler.dispatchMessage(Handler.java:99)08-28 15:02:26.632:E / AndroidRuntime(331):在android.os.Looper。 loop(Looper.java:123)08-28 15:02:26.632:E / AndroidRuntime(331):在android.app.ActivityThread.main(ActivityThread.java:4627)08-28 15:02:26.632:E / AndroidRuntime(331):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)08-28 15:02:26.632:E / AndroidRuntime(331):at com.android.internal.os .ZygoteInit.main(ZygoteInit.java:626)08-28 15:02:26.632:E / AndroidRuntime(331):at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)08-28 15:02 :26.632: E/AndroidRuntime(331): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 08-28 15:02:26.632: E/AndroidRuntime(331): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) :26.632:E / AndroidRuntime(331):在android.app.Instrumentation.newActivity(Instrumentation.java:1021)08-28 15:02:26.632:E / AndroidRuntime(331):at android.app.ActivityThread.performLaunchActivity( ActivityThread.java:2577)

The problem is probably here: 问题可能出在这里:

String dawaaList[] = {emportPref.getString("subject", null)};
setListAdapter(new ArrayAdapter<String>(DawaaActivity.this,
android.R.layout.simple_list_item_1, dawaaList));

If you don't have any subject you will get a null and then probably a nullpointer somewhere. 如果你没有任何主题,你将得到一个null,然后可能是一个nullpointer。

Try to check the null, or initialize it with something if null, like: 尝试检查null,或者使用null来初始化它,如:

if (dawaaList[] == null) { 
   new String[]{"No subjects"}; 
}

and please, post your logcat for further help. 并请,发布您的logcat以获得进一步的帮助。

EDIT: 编辑:

You can handle it this way: 你可以这样处理它:

just add a ListView in your layout 只需在布局中添加ListView即可

    <ListView
        android:id="@+id/myListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

and populate it in your Activity 并在您的活动中填充它

    setContentView(R.layout.main);
    ListView listView = (ListView) findViewById(R.id.myListView);
    SharedPreferences pref = getSharedPreferences("dawaaData", MODE_PRIVATE);
    String items = pref.getString("subject", "item1,item2,item3");
    String[] listItems = items.split(",");
    listView.setAdapter(new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, listItems));

I've choose a comma as a delimiter because SharedPreferences cannot store arrays. 我选择逗号作为分隔符,因为SharedPreferences无法存储数组。 You should use a Set and pref.getStringSet (from api 11), or serialize the list (json o anything). 您应该使用Set和pref.getStringSet(来自api 11),或序列化列表(json o anything)。

The App A gets closed because the first time you dont have initialized the values. App A因为第一次没有初始化值而关闭。 The simplest way is to wrap the code that read the value from the SharedPreferences in a try catch(Throwable t) 最简单的方法是在try catch中包装从SharedPreferences中读取值的代码(Throwable t)

This two functions will solve your problem, First one is to save preference that you will need to cal in class A, where you want to store value 这两个函数将解决您的问题,第一个是保存您需要在A类中进行cal的首选项,您要在其中存储值

public void saveSharedPreference() {
        SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
                MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = myPrefs.edit();
        prefsEditor.putString(stringname, stringvalue);
        prefsEditor.commit();
    }

And on your B activity, you have to call LoadPreferences function 在B活动中,您必须调用LoadPreferences函数

private void LoadPreferences() {
        SharedPreferences sharedPreferences = this.getSharedPreferences(
                "myPrefs", MODE_WORLD_READABLE);
        String yourvariable= sharedPreferences.getString(name, "0"); // it will take 0 as default value if variable is empty and you can set you own default value  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM