我正在尝试(在点击CALCULATE按钮时)从editText中提取内容(单词/字母)并将它们存储在一个数组中,该数组将继续使用我所拥有的计数器添加到自身。我觉得我有从MainActivity的数组传递到详细的细节,但每次我试图以通常的方式达到细节时崩溃。这是代码。
提取编辑文本的内容并将其存储在下一个阵列位置
String explanationItem = expenseExplanation.getText().toString();
ExplanationDetailArray[placeCounterExplanation] = explanationItem;
placeCounterExplanation = placeCounterExplanation + 1;
将阵列存储/发送到下一个活动
Intent intent = new Intent(MainActivity.this, detailactivity.class);
Bundle myBundle = new Bundle();
myBundle.putStringArray("explanations", ExplanationDetailArray);
intent.putExtras(myBundle);
startActivity(intent);
从下一次活动中接收
Intent intent = getIntent();
Bundle myBundle = intent.getExtras();
String[] testArray = myBundle.getStringArray("explanations");
LOG CAT ERROR
08-26 18:30:07.894: E/AndroidRuntime(9652): FATAL EXCEPTION: main
08-26 18:30:07.894: E/AndroidRuntime(9652): Process: com.example.forecastspending, PID: 9652
08-26 18:30:07.894: E/AndroidRuntime(9652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.forecastspending/com.example.forecastspending.detailactivity}: java.lang.NullPointerException
08-26 18:30:07.894: E/AndroidRuntime(9652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
08-26 18:30:07.894: E/AndroidRuntime(9652): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
08-26 18:30:07.894: E/AndroidRuntime(9652): at android.app.ActivityThread.access$800(ActivityThread.java:139)
08-26 18:30:07.894: E/AndroidRuntime(9652): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
08-26 18:30:07.894: E/AndroidRuntime(9652): at android.os.Handler.dispatchMessage(Handler.java:102)
08-26 18:30:07.894: E/AndroidRuntime(9652): at android.os.Looper.loop(Looper.java:136)
08-26 18:30:07.894: E/AndroidRuntime(9652): at android.app.ActivityThread.main(ActivityThread.java:5105)
08-26 18:30:07.894: E/AndroidRuntime(9652): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 18:30:07.894: E/AndroidRuntime(9652): at java.lang.reflect.Method.invoke(Method.java:515)
08-26 18:30:07.894: E/AndroidRuntime(9652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
08-26 18:30:07.894: E/AndroidRuntime(9652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
08-26 18:30:07.894: E/AndroidRuntime(9652): at dalvik.system.NativeStart.main(Native Method)
08-26 18:30:07.894: E/AndroidRuntime(9652): Caused by: java.lang.NullPointerException
08-26 18:30:07.894: E/AndroidRuntime(9652): at com.example.forecastspending.detailactivity.onCreate(detailactivity.java:66)
08-26 18:30:07.894: E/AndroidRuntime(9652): at android.app.Activity.performCreate(Activity.java:5275)
08-26 18:30:07.894: E/AndroidRuntime(9652): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-26 18:30:07.894: E/AndroidRuntime(9652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
08-26 18:30:07.894: E/AndroidRuntime(9652): ... 11 more
存储方法:
if (spotCounter == 0){
editor.putInt("array_size", ExplanationDetailArray.length);
for(int i=0;i<ExplanationDetailArray.length; i++)
editor.putString("array_" + i, ExplanationDetailArray[i]);
//spotCounter = spotCounter + 1;
editor.commit();
}
editor.putInt("spotcounter" ,spotCounter);
editor.commit();
接收方法:
SharedPreferences pref = getApplicationContext()。getSharedPreferences(&#34; MyPref&#34;,0); // 0 - 用于私人模式 编辑器编辑器= pref.edit();
int spotCounter = pref.getInt("spotcounter", 0);
if(spotCounter == 0){
int size = pref.getInt("array_size", 0);
ExplanationDetailArray = new String[size + 1];
ExplanationDetailArray[0] = pref.getString("array_" + 0, null);
}
if(spotCounter > 0){
int size = pref.getInt("array_size", 0);
ExplanationDetailArray = new String[size + 1];
ExplanationDetailArray[spotCounter] = pref.getString("array_" + spotCounter, null);
}
spotCounter = spotCounter + 1;
editor.putInt("spotcounter" ,spotCounter);
editor.commit();