我试图使该拿的EditText的价值,并把它放在TextView的另一个动作的活动。
这是包含一个TextView和选项菜单进行编辑的第一个活动代码
public class Saturday extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saturday);
Intent i = getIntent();
String txt = i.getExtras().getString("txtData","");
final TextView Tclass1;
Tclass1 = (TextView)findViewById(R.id.seven);
Tclass1.setText(txt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.saturday, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.menu.saturday:
edit_schedule();
return true;
}
return super.onOptionsItemSelected(item);
}
public void edit_schedule() {
final TextView Tclass1;
Tclass1 = (TextView)findViewById(R.id.seven);
Intent i = new Intent(this, EditSaturday.class);
i.putExtra("text" , Tclass1.getText().toString());
startActivity(i);
}}
这是其他活动包含与保存按钮,一个EditText代码
public class EditSaturday extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_saturday);
Intent i = getIntent();
String txt = i.getExtras().getString("txtData","");
final EditText Eclass1;
Eclass1 = (EditText)findViewById(R.id.eseven);
Eclass1.setText(txt);
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save();
}});
}
public void save() {
final EditText Eclass1;
Eclass1 = (EditText)findViewById(R.id.eseven);
Intent i = new Intent(this, Saturday.class);
i.putExtra("text" , Eclass1.getText().toString());
startActivity(i);
}}
我运行该应用程序后,它给我一个错误
这是logcat的
04-14 08:15:17.911: E/AndroidRuntime(1202): FATAL EXCEPTION: main
04-14 08:15:17.911: E/AndroidRuntime(1202): java.lang.RuntimeException: Unable to start activity ComponentInfo{sarah.bodoor.sto/sarah.bodoor.sto.Saturday}: java.lang.NullPointerException
04-14 08:15:17.911: E/AndroidRuntime(1202): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-14 08:15:17.911: E/AndroidRuntime(1202): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-14 08:15:17.911: E/AndroidRuntime(1202): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-14 08:15:17.911: E/AndroidRuntime(1202): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-14 08:15:17.911: E/AndroidRuntime(1202): at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 08:15:17.911: E/AndroidRuntime(1202): at android.os.Looper.loop(Looper.java:137)
04-14 08:15:17.911: E/AndroidRuntime(1202): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-14 08:15:17.911: E/AndroidRuntime(1202): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 08:15:17.911: E/AndroidRuntime(1202): at java.lang.reflect.Method.invoke(Method.java:511)
04-14 08:15:17.911: E/AndroidRuntime(1202): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-14 08:15:17.911: E/AndroidRuntime(1202): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-14 08:15:17.911: E/AndroidRuntime(1202): at dalvik.system.NativeStart.main(Native Method)
04-14 08:15:17.911: E/AndroidRuntime(1202): Caused by: java.lang.NullPointerException
04-14 08:15:17.911: E/AndroidRuntime(1202): at sarah.bodoor.sto.Saturday.onCreate(Saturday.java:22)
04-14 08:15:17.911: E/AndroidRuntime(1202): at android.app.Activity.performCreate(Activity.java:5104)
04-14 08:15:17.911: E/AndroidRuntime(1202): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-14 08:15:17.911: E/AndroidRuntime(1202): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-14 08:15:17.911: E/AndroidRuntime(1202): ... 11 more