Android 字符串[]返回空值
我有4个字符串数组,我正在将其转换为单个字符串(以、)并保存在SharedPreference中。然后,我在onCreate方法中检索变量。然后我想检查元素是否有赋值,如果有,用静态生成的textview显示:Android 字符串[]返回空值,android,arrays,sharedpreferences,Android,Arrays,Sharedpreferences,我有4个字符串数组,我正在将其转换为单个字符串(以、)并保存在SharedPreference中。然后,我在onCreate方法中检索变量。然后我想检查元素是否有赋值,如果有,用静态生成的textview显示: public class DebtList extends Activity { @TargetApi(Build.VERSION_CODES.HONEYCOMB) protected void onCreate(Bundle savedInstanceState) { sup
public class DebtList extends Activity {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debtlist);
String[] debtName = new String[10];
String[] debtAmount = new String[10];
String[] debtRate = new String[10];
String[] debtPayment = new String[10];
SharedPreferences sharedPref= getSharedPreferences("chaosdatasnowball", 0);
String tempDebtNames = sharedPref.getString("debtNames", "");
if (!tempDebtNames.equals("")){
debtName = convertStringToArray(tempDebtNames);
}
String tempDebtAmounts = sharedPref.getString("debtAmounts", "");
if (!tempDebtAmounts.equals("")){
debtName = convertStringToArray(tempDebtAmounts);
}
String tempDebtRates = sharedPref.getString("debtRates", "");
if (!tempDebtRates.equals("")){
debtName = convertStringToArray(tempDebtRates);
}
String tempDebtPayments = sharedPref.getString("debtPayments", "");
if (!tempDebtPayments.equals("")){
debtName = convertStringToArray(tempDebtPayments);
}
Bundle extras = getIntent().getExtras();
int trigger = 0;
for (int i=0;i<debtName.length;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<debtName.length;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();
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));
trlp.span = 3;
tr.setLayoutParams(trlp);
tv0.setText("" + debtName[i]);
tv1.setText("" + debtAmount[i]);
tv2.setText("" + debtPayment[i]);
tv3.setText("" + i);
tr.addView(tv0);
tr.addView(tv1);
tr.addView(tv2);
tr.addView(tv3);
tl.addView(tr);
}
}
int counter = debtName.length;
SharedPreferences.Editor editor= sharedPref.edit();
String debtNames = convertArrayToString(debtName, counter);
editor.putString("debtNames", debtNames);
String debtAmounts = convertArrayToString(debtAmount, counter);
editor.putString("debtAmounts", debtAmounts);
String debtRates = convertArrayToString(debtRate, counter);
editor.putString("debtRates", debtRates);
String debtPayments = convertArrayToString(debtPayment, counter);
editor.putString("debtPayments", debtPayments);
editor.putInt("counter", counter);
editor.commit();
TextView disp = (TextView) findViewById(R.id.dispAdditionalAmount);
disp.setText("" + debtNames);
}
public static String convertArrayToString(String[] array, int stop){
String str = "";
for (int i = 0;i<stop; i++) {
str = str+array[i];
// Do not append comma at the end of last element
if(i<stop-1){
str = str+",";
}
}
return str;
}
public static String[] convertStringToArray(String str){
String[] arr = str.split(",");
return arr;
}
公共类债务列表扩展活动{
@TargetApi(构建版本代码蜂窝)
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.debtlist);
字符串[]债务人名称=新字符串[10];
字符串[]债务金额=新字符串[10];
字符串[]债务率=新字符串[10];
字符串[]债务人付款=新字符串[10];
SharedReferences SharedReferences=GetSharedReferences(“chaosdatasnowball”,0);
String tempDebtNames=sharedPref.getString(“债务人名称”和“”);
如果(!tempDebtNames.equals(“”){
debtName=convertStringToArray(tempDebtName);
}
String tempDebtAmounts=sharedPref.getString(“debtAmounts”和“”);
如果(!tempDebtAmounts.equals(“”){
debtName=convertStringToArray(临时债务金额);
}
String tempDebtRates=sharedPref.getString(“debtRates”,”);
如果(!tempDebtRates.equals(“”){
debtName=convertStringToArray(临时债务利率);
}
字符串tempDebtPayments=sharedPref.getString(“debtPayments”和“”);
如果(!tempDebtPayments.equals(“”){
债务人名称=convertStringToArray(临时债务付款);
}
Bundle extras=getIntent().getExtras();
int触发器=0;
对于(int i=0;i可能是因为您没有设置活动的ContentView。请确保您的首选项没有返回null。声明内容视图时,它只是没有复制到此处-更新以显示完整的活动还解释哪些行返回null。@JoxTraex-当“tv0.setText(“+debtName[i])正在处理中,它在4列10行中显示“null”。