i am trying to save the data in list view using Shared Preferences but im getting some errors. i have 2 classes one is main activity and 2nd is editbox.
我正在尝试使用共享的首选项将数据保存到列表视图中,但是我得到了一些错误。我有两个班一个是主要活动,第二个是editbox。
MainActivity.java
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
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;
}
}
These are the errors
这些都是错误
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
2 个解决方案
#1
1
Remove comment from
删除评论
nameText=(EditText)findViewById(R.id.editText1);
You setText
to nameText
without initialized it (EditText)
.
在没有初始化nameText (EditText)的情况下,将其设置为nameText。
#2
2
You have this
你有这个
nameText.setText(setting.getString("List 1", "poss"));
But you have not initialized nameText. Its commented
但是还没有初始化nameText。它的评论
//nameText=(EditText)findViewById(R.id.editText1)
#1
1
Remove comment from
删除评论
nameText=(EditText)findViewById(R.id.editText1);
You setText
to nameText
without initialized it (EditText)
.
在没有初始化nameText (EditText)的情况下,将其设置为nameText。
#2
2
You have this
你有这个
nameText.setText(setting.getString("List 1", "poss"));
But you have not initialized nameText. Its commented
但是还没有初始化nameText。它的评论
//nameText=(EditText)findViewById(R.id.editText1)