当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Saving String Arrays with SharedPreference

问题描述:

I have 4 arrays I want to be able to save internally to the device:

String[] debtName = new String[10];

String[] debtAmount = new String[10];

String[] debtRate = new String[10];

String[] debtPayment = new String[10];

My full onCreate is here:

package biz.midl.debtsnowball;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

import android.annotation.TargetApi;

import android.app.Activity;

import android.content.SharedPreferences;

import android.os.Build;

import android.os.Bundle;

import android.view.Menu;

import android.widget.TableLayout;

import android.widget.TableRow;

import android.widget.TableRow.LayoutParams;

import android.widget.TextView;

public class DebtList<T> extends Activity {

String[] debtName = new String[10];

String[] debtAmount = new String[10];

String[] debtRate = new String[10];

String[] debtPayment = new String[10];

@TargetApi(Build.VERSION_CODES.HONEYCOMB)

@SuppressWarnings({ "rawtypes", "unchecked" })

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.debtlist);

Bundle extras = getIntent().getExtras();

int trigger = 0;

//Load Data

SharedPreferences sharedPrefs= getSharedPreferences("chaosdata", 0);

Set<String> retrievedName = sharedPrefs.getStringSet("debtNames", null);

List<String> list = new ArrayList<String>(retrievedName); //Line 41 where it appears the issues is originating.

for(int i = 0; i < list.size(); i++) debtName[i] = list.get(i);

//End Load

for (int i=0;i<10;i++)

{

if (debtName[i] == null && extras != null && trigger==0)

{

debtName[i] = extras.getString("debtName");

debtAmount[i] = extras.getString("debtAmount");

debtRate[i] = extras.getString("debtRate");

debtPayment[i] = extras.getString("debtPayment");

trigger = 1;

}

}

TableLayout tl = (TableLayout) findViewById(R.id.debtListTableView);

for (int i=0;i<10;i++)

{

if (debtName[i] != null)

{

TableRow tr = new TableRow(this);

TextView tv0 = new TextView(this);

TextView tv1 = new TextView(this);

TextView tv2 = new TextView(this);

TextView tv3 = new TextView(this);

TableRow.LayoutParams trlp = new TableRow.LayoutParams();

//LayoutParams tvlp = new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f);

tv0.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f));

tv1.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f));

tv2.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f));

tv3.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f));

/**

tv0.setLayoutParams(tvlp);

tv1.setLayoutParams(tvlp);

tv2.setLayoutParams(tvlp);

**/

trlp.span = 3;

tr.setLayoutParams(trlp);

tv0.setText("" + debtName[i]);

tv1.setText("" + debtAmount[i]);

tv2.setText("" + debtPayment[i]);

tv3.setText("Holder");

tr.addView(tv0);

tr.addView(tv1);

tr.addView(tv2);

tr.addView(tv3);

tl.addView(tr);

}

}

List listName = Arrays.asList(debtName);

Set setName = new HashSet(listName);

//Save Data

SharedPreferences.Editor editor= sharedPrefs.edit();

editor.putStringSet("debtNames", setName);

editor.commit();

//End Save

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.debt_list, menu);

return true;

}

}

This worked once to save and load my arrays. When I did a user data wipe, it stopped working. Below is the LogCat:

11-03 20:47:31.485: V/TLINE(516): new: [email protected]

11-03 20:47:32.005: V/TLINE(516): new: [email protected]

11-03 20:47:34.745: D/dalvikvm(516): GC_CONCURRENT freed 127K, 4% free 9004K/9287K, paused 6ms+12ms

11-03 20:47:37.336: D/AndroidRuntime(516): Shutting down VM

11-03 20:47:37.336: W/dalvikvm(516): threadid=1: thread exiting with uncaught exception (group=0x40014760)

11-03 20:47:37.356: E/AndroidRuntime(516): FATAL EXCEPTION: main

11-03 20:47:37.356: E/AndroidRuntime(516): java.lang.RuntimeException: Unable to start activity ComponentInfo{biz.midl.debtsnowball/biz.midl.debtsnowball.DebtList}: java.lang.NullPointerException

11-03 20:47:37.356: E/AndroidRuntime(516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)

11-03 20:47:37.356: E/AndroidRuntime(516): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)

11-03 20:47:37.356: E/AndroidRuntime(516): at android.app.ActivityThread.access$1500(ActivityThread.java:122)

11-03 20:47:37.356: E/AndroidRuntime(516): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)

11-03 20:47:37.356: E/AndroidRuntime(516): at android.os.Handler.dispatchMessage(Handler.java:99)

11-03 20:47:37.356: E/AndroidRuntime(516): at android.os.Looper.loop(Looper.java:132)

11-03 20:47:37.356: E/AndroidRuntime(516): at android.app.ActivityThread.main(ActivityThread.java:4025)

11-03 20:47:37.356: E/AndroidRuntime(516): at java.lang.reflect.Method.invokeNative(Native Method)

11-03 20:47:37.356: E/AndroidRuntime(516): at java.lang.reflect.Method.invoke(Method.java:491)

11-03 20:47:37.356: E/AndroidRuntime(516): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)

11-03 20:47:37.356: E/AndroidRuntime(516): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)

11-03 20:47:37.356: E/AndroidRuntime(516): at dalvik.system.NativeStart.main(Native Method)

11-03 20:47:37.356: E/AndroidRuntime(516): Caused by: java.lang.NullPointerException

11-03 20:47:37.356: E/AndroidRuntime(516): at java.util.ArrayList.<init>(ArrayList.java:97)

11-03 20:47:37.356: E/AndroidRuntime(516): at biz.midl.debtsnowball.DebtList.onCreate(DebtList.java:41)

11-03 20:47:37.356: E/AndroidRuntime(516): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)

11-03 20:47:37.356: E/AndroidRuntime(516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)

11-03 20:47:37.356: E/AndroidRuntime(516): ... 11 more

11-03 20:47:38.876: I/Process(516): Sending signal. PID: 516 SIG: 9

网友答案:

Convert the array to a set and store it directly:

SharedPreferences.Editor editor= sharedPref.edit();
Set<T> debtSet = new HashSet<T>(Arrays.asList(debtNames));
editor.putStringSet("debtNames", debtSet);

Use debtSet.toArray() when you retrieve it.

To retrieve:

debtNames = sharedPrefs.getStringSet("debtNames").toArray();

or

debtSet = sharedPrefs.getStringSet("debtNames");
debtNames = debtSet.toArray();

In general, arrays are tough to work with. You might consider using Set for everything, especially ArrayList<String>

To store an Arraylist without having to convert it to/from a Set:

editor.putString("debtNames", ObjectSerializer.serialize(debtNames));

Where debtNames is ArrayList<String>

To retrieve an ArrayList:

debtNames = (ArrayList<String) ObjectSerializer.deserialize(prefs.getString("debtNames", ObjectSerializer.serialize(new ArrayList<String>())));

You can get ObjectSerializer here or use some other serialisation class.

Note that I am using a new Arraylist as the default value.

As to your null pointer, I can't see where you calleditor.commit(). If you are not doing this, your values are never saved to ShardPreference.

分享给朋友:
您可能感兴趣的文章:
随机阅读: