我正在创建一个应用程序,您可以在其中添加列表中的项目。还会保存列表。列表中的添加项目正在运行,我认为保存部分存在问题 import java.util.ArrayList;
import java.util.ArrayList;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.app.Activity;
import android.content.SharedPreferences;
public class MainActivity extends Activity {
public static String file = "file";
SharedPreferences data= getSharedPreferences(file, 0);
public ArrayList<String> ListItems = new ArrayList<String>();
public int size = ListItems.size();
public SharedPreferences.Editor editor = data.edit();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myListView = (ListView) findViewById(R.id.listView1);
final EditText myEditText = (EditText) findViewById(R.id.editText1);
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ListItems);
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_ENTER) {
ListItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
editor.putInt("list_size", size);
for (int i = 0; i < size; i++) {
editor.putString("list_" + i,
(String) ListItems.get(i));
}
editor.commit();
return true;
}
return false;
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
int size1 = (data.getInt("list_size", 0));
for (int i = 0; i < size1; i++) {
ListItems.set(i,
(data.getString("list_" + i, "error loading this item")));
}
editor.commit();
}
}
错误日志:
05-07 17:30:47.678: E/AndroidRuntime(2162): FATAL EXCEPTION: main
05-07 17:30:47.678: E/AndroidRuntime(2162): Process: com.paramdevelopment.pizzamaking, PID: 2162
05-07 17:30:47.678: E/AndroidRuntime(2162): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.paramdevelopment.pizzamaking/com.paramdevelopment.pizzamaking.MainActivity}: java.lang.NullPointerException
05-07 17:30:47.678: E/AndroidRuntime(2162): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-07 17:30:47.678: E/AndroidRuntime(2162): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-07 17:30:47.678: E/AndroidRuntime(2162): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-07 17:30:47.678: E/AndroidRuntime(2162): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-07 17:30:47.678: E/AndroidRuntime(2162): at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 17:30:47.678: E/AndroidRuntime(2162): at android.os.Looper.loop(Looper.java:136)
05-07 17:30:47.678: E/AndroidRuntime(2162): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-07 17:30:47.678: E/AndroidRuntime(2162): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 17:30:47.678: E/AndroidRuntime(2162): at java.lang.reflect.Method.invoke(Method.java:515)
05-07 17:30:47.678: E/AndroidRuntime(2162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-07 17:30:47.678: E/AndroidRuntime(2162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-07 17:30:47.678: E/AndroidRuntime(2162): at dalvik.system.NativeStart.main(Native Method)
05-07 17:30:47.678: E/AndroidRuntime(2162): Caused by: java.lang.NullPointerException
05-07 17:30:47.678: E/AndroidRuntime(2162): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173)
05-07 17:30:47.678: E/AndroidRuntime(2162): at com.paramdevelopment.pizzamaking.MainActivity.<init>(MainActivity.java:16)
05-07 17:30:47.678: E/AndroidRuntime(2162): at java.lang.Class.newInstanceImpl(Native Method)
05-07 17:30:47.678: E/AndroidRuntime(2162): at java.lang.Class.newInstance(Class.java:1208)
05-07 17:30:47.678: E/AndroidRuntime(2162): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-07 17:30:47.678: E/AndroidRuntime(2162): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
05-07 17:30:47.678: E/AndroidRuntime(2162): ... 11 more
答案 0 :(得分:0)
您正在尝试使用未初始化的SharedPrefferences数据,并在后续步骤中使用它们。
关键线是:
SharedPreferences data;
您已声明数据但数据未初始化,并且在您尝试使用它的代码中(数据值为空),这会导致NullPointerException
。
此行产生错误:
public SharedPreferences.Editor editor = data.edit();
您必须在使用它之前初始化变量,就像在onCreate方法中一样。
编辑1 :(基于OP编辑和评论)
正如我之前所说,SharedPrefferences
为空,你有问题。您必须正确初始化SharedPrefferences
。
您正在尝试使用编辑器而未初始化SharedPrefferences
。
您可以从许多地方了解如何使用SharedPrefferences
,但很快您就可以查看here。
答案 1 :(得分:0)
您可以在将活动用作上下文之前过早访问SharedPreferences
。将共享首选项init移至onCreate()
。