的EditText和文本视图之间传输数据(Transfer data between EditTex

2019-10-30 09:34发布

我试图使该拿的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

Answer 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);

在你的第二个actiivty的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); 
}  


Answer 2:

尝试执行这段代码。

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

用。。。来代替

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

因为你需要从传球意图获取值相同的密钥。

你把键值“文本”时传意图和使用键“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);

}


文章来源: Transfer data between EditText and Text View