在EditText和Text View之间传输数据

时间:2013-04-14 08:30:17

标签: android textview android-edittext runtimeexception

我正在尝试创建一个带有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

2 个答案:

答案 0 :(得分:1)

Intent i = new Intent(this, Saturday.class);

应该是

Intent i = new Intent(EditSaturday.this, Saturday.class); 
// should be referring to activity context

在你的保存()

另外

   i.putExtra("text" , Eclass1.getText().toString());// key in EditSaturday 
   // keys should be the same 
   String txt = i.getExtras().getString("txtData","");// key in saturday
   // keys are different

在您的第一个活动中,点击按钮时获取editext值,并使用意图在活动之间传递数据

EditText et= (EditText)findviewById(R.id.edittext);
String s= et.getText().toString();
Intent i= new Intent(firstActivity.this,secondActivity.class);
i.putExtra("key",s);
startActivity(i);

在你的第二个活动onCreate()

setContentView(R.layout.second); 
Bundle extras= getIntent().getExtras();
if(extras!=null)
{
  TextView tv= (TextView)findviewById(R.id.textView1);
  String value = extras.getString("key");
  tv.setText(value); 
}  

答案 1 :(得分:0)

尝试实现此代码。

String txt = i.getExtras().getString("text","");

替换为

String txt = i.getExtras().getString("txtData","");

因为你需要相同的密钥才能从传递意图中获取价值。

你在传递意图时输入关键字“text”,并在获得两者不同的意图时使用关键字“txtData”,使用相同的关键敌人传递和获取。

使用以下代码通过选项菜单调用下一个活动后单击。

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    menu.add(0, 0, 0, "Next");
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
 switch(item.getItemId())
 {
 case 0:
     edit_schedule();
         return true;
 }
 return super.onOptionsItemSelected(item);

}