我有2个活动类,“Inset_plate_number”类和“CarSelection”类。我试图在“Insert_plate_number”类的文本框中写一些内容然后将我写入的内容放入ArrayList然后存储在sharedpreferences中。接下来,我想检索存储在sharedpreferences中的东西,并在“CarSelection”类的微调器中显示它们。
但是每次运行它都会给我NullPointerException错误。 arraylist是否有问题,存储共享偏好,加载共享首选项或在微调器上显示?
Insert_plate_number类,
public class Insert_plate_number extends ActionBarActivity {
static ArrayList<String> list;
static SharedPreferences sharedpreferences;
static String combo;
public static final String MyPREFERENCES = "MyPREFERENCES";
static int size;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_insert_plate_number);
list = new ArrayList<String>();
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
}
public void onClick_Save(View view) {
Editor editor = sharedpreferences.edit();
EditText plate_number = (EditText) findViewById(R.id.car_plate_number);
EditText phone_number = (EditText) findViewById(R.id.phone_number);
String phoneNo = phone_number.getText().toString();
String plateNo = plate_number.getText().toString();
combo = plateNo + "," + phoneNo;
list.add(combo);
size = list.size();
editor.putInt("list_size", size);
for (int i = 0; i <size; i++) {
editor.putString("list_"+i, list.get(i));
}
editor.commit();
}
}
CarSelection类,
public class CarSelection extends ActionBarActivity {
Spinner spinner;
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_car_selection);
sharedpreferences = getSharedPreferences(Insert_plate_number.MyPREFERENCES, Context.MODE_PRIVATE);
int arraysize = sharedpreferences.getInt("list_size", 0);
for (int i = 0; i <arraysize; i++) {
sharedpreferences.getString("list_"+i, null);
}
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adp = new ArrayAdapter<String>(CarSelection.this,android.R.layout.simple_spinner_item, Insert_plate_number.list);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
}
}
logcat的
06-07 12:52:01.640: E/AndroidRuntime(979): FATAL EXCEPTION: main
06-07 12:52:01.640: E/AndroidRuntime(979): Process: com.example.myfirstapp, PID: 979
06-07 12:52:01.640: E/AndroidRuntime(979): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.CarSelection}: java.lang.NullPointerException
06-07 12:52:01.640: E/AndroidRuntime(979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-07 12:52:01.640: E/AndroidRuntime(979): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-07 12:52:01.640: E/AndroidRuntime(979): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-07 12:52:01.640: E/AndroidRuntime(979): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-07 12:52:01.640: E/AndroidRuntime(979): at android.os.Handler.dispatchMessage(Handler.java:102)
06-07 12:52:01.640: E/AndroidRuntime(979): at android.os.Looper.loop(Looper.java:136)
06-07 12:52:01.640: E/AndroidRuntime(979): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-07 12:52:01.640: E/AndroidRuntime(979): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 12:52:01.640: E/AndroidRuntime(979): at java.lang.reflect.Method.invoke(Method.java:515)
06-07 12:52:01.640: E/AndroidRuntime(979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-07 12:52:01.640: E/AndroidRuntime(979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-07 12:52:01.640: E/AndroidRuntime(979): at dalvik.system.NativeStart.main(Native Method)
06-07 12:52:01.640: E/AndroidRuntime(979): Caused by: java.lang.NullPointerException
06-07 12:52:01.640: E/AndroidRuntime(979): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
06-07 12:52:01.640: E/AndroidRuntime(979): at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:114)
06-07 12:52:01.640: E/AndroidRuntime(979): at android.widget.Spinner.setAdapter(Spinner.java:413)
06-07 12:52:01.640: E/AndroidRuntime(979): at com.example.myfirstapp.CarSelection.onCreate(CarSelection.java:33)
06-07 12:52:01.640: E/AndroidRuntime(979): at android.app.Activity.performCreate(Activity.java:5231)
06-07 12:52:01.640: E/AndroidRuntime(979): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-07 12:52:01.640: E/AndroidRuntime(979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-07 12:52:01.640: E/AndroidRuntime(979): ... 11 more
答案 0 :(得分:0)
sharedpreferences.getString(&#34; list _&#34; + i,null);更改null会导致错误..
答案 1 :(得分:0)
这里的问题是您正在尝试访问另一个可能为null的类的ArrayList。
实现目标的最佳方法是在onCreate的CarSelection.class中执行此操作:
sharedpreferences = getSharedPreferences(Insert_plate_number.MyPREFERENCES, Context.MODE_PRIVATE);
// You need to create an Array list and fill it with string you have in SharedPreferences
ArrayList<String> list;
list = new ArrayList<String>();
int arraysize = sharedpreferences.getInt("list_size", 0);
for (int i = 0; i <arraysize; i++) {
list.add(sharedpreferences.getString("list_"+i, ""));
}
Spinner spinner = (Spinner) findViewById(R.id.edit_text);
ArrayAdapter<String> adp = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);