我正在尝试使用“共享首选项”将列表视图中的数据保存,但是我遇到了一些错误。 我有2个班级,一个是主要活动,第二个是编辑框。
MainActivity.java
public class MainActivity extends Activity
{
ListView listView1=null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView1 = (ListView) findViewById(R.id.listView1);
String[] items = { "List 1", "List 2", "List 3", "List 4" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//String item = ((TextView)view).getText().toString();
//Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
//int pos = (int) listView1.getItemIdAtPosition(position);
String value = listView1.getAdapter().getItem(position).toString();
Intent it1 = new Intent(getApplicationContext(), editbox.class);
it1.putExtra("index", value );
startActivity(it1);
}
});
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add("List").setIntent(new Intent(this,MainActivity.class));
return true;
}
}
editbox.java
public class editbox extends Activity
{
private TextView tv;
private EditText nameText,nameText1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.editbox);
//tv = (TextView) findViewById(R.id.textView1);
//Bundle b = getIntent().getExtras();
//int poss = b.getInt( "index");
// tv.setText("Position = " + poss);
//Bundle extras=getIntent().getExtras();
//Intent it = getIntent();
//int myvalue = it.getIntExtra("id",0);
//Bundle extras = it.getExtras();
//String myvalue = extras.getString("data");
//nameText=(EditText)findViewById(R.id.editText1);
//nameText.setText(poss);
Bundle b = getIntent().getExtras();
String val = b.getString("index");
//String val = b.getStringExtra("index");
SharedPreferences setting = getSharedPreferences("List 1", 0);
nameText.setText(setting.getString("List 1", "poss"));
//nameText.setText(poss);
//display=(EditText)findViewById(R.id.editText1);
//display.setText(""+ myvalue);
}
protected void onStop()
{
super.onStop();
SharedPreferences setting = getSharedPreferences("List 1",0);
SharedPreferences.Editor editor = setting.edit();
editor.putString("List 1", nameText.getText().toString());
editor.commit();
}
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu);
menu.add("List 1").setIntent(new Intent(this,MainActivity.class));
return true;
}
}
这些是错误
03-20 01:03:29.786: E/AndroidRuntime(1272): FATAL EXCEPTION: main
03-20 01:03:29.786: E/AndroidRuntime(1272): Process: com.example.list, PID: 1272
03-20 01:03:29.786: E/AndroidRuntime(1272): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.list/com.example.list.editbox}: java.lang.NullPointerException
03-20 01:03:29.786: E/AndroidRuntime(1272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-20 01:03:29.786: E/AndroidRuntime(1272): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-20 01:03:29.786: E/AndroidRuntime(1272): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-20 01:03:29.786: E/AndroidRuntime(1272): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-20 01:03:29.786: E/AndroidRuntime(1272): at android.os.Handler.dispatchMessage(Handler.java:102)
03-20 01:03:29.786: E/AndroidRuntime(1272): at android.os.Looper.loop(Looper.java:136)
03-20 01:03:29.786: E/AndroidRuntime(1272): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-20 01:03:29.786: E/AndroidRuntime(1272): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 01:03:29.786: E/AndroidRuntime(1272): at java.lang.reflect.Method.invoke(Method.java:515)
03-20 01:03:29.786: E/AndroidRuntime(1272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-20 01:03:29.786: E/AndroidRuntime(1272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-20 01:03:29.786: E/AndroidRuntime(1272): at dalvik.system.NativeStart.main(Native Method)
03-20 01:03:29.786: E/AndroidRuntime(1272): Caused by: java.lang.NullPointerException
03-20 01:03:29.786: E/AndroidRuntime(1272): at com.example.list.editbox.onCreate(editbox.java:53)
03-20 01:03:29.786: E/AndroidRuntime(1272): at android.app.Activity.performCreate(Activity.java:5231)
03-20 01:03:29.786: E/AndroidRuntime(1272): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-20 01:03:29.786: E/AndroidRuntime(1272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-20 01:03:29.786: E/AndroidRuntime(1272): ... 11 more
答案 0 :(得分:2)
你有这个
nameText.setText(setting.getString("List 1", "poss"));
但是你还没有初始化nameText。其评论
//nameText=(EditText)findViewById(R.id.editText1)
答案 1 :(得分:1)
从
中删除评论nameText=(EditText)findViewById(R.id.editText1);
您setText
至nameText
未初始化(EditText)
。