1

我想在editText1(Activity1)中输入相同的文本到editText2(Activity2)

在活动 1 中:

public class Activity1 extends Activity {

    editText1 = (EditText) findViewById(R.id.editText1);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all1);
    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()) {
            case R.id.settings:
                Intent intent = new Intent(Activity1.this, Activity2.class);
                intent.putExtra("editTextentered", editText1.getText().toString()); 
                startActivity(intent);  
                return true;
            default:
                return super.onOptionsItemSelected(item);

        }
    }
}

在活动 2 中:

public class Activity2 extends Activity {

    private EditText editText1;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        editText1 = (EditText) findViewById(R.id.editText2);
        setContentView(R.layout.settings);
        Bundle extras = getIntent().getExtras(); 
        String editTextentered = null;

        if(extras !=null && extras.containsKey("editTextentered")) {
            editTextVal= extras.getString("editTextentered");
        }
        editText1.setText(""+editTextentered);
    }
}

我正在尝试调试应用程序,但是它崩溃了

4

5 回答 5

1

尝试这个;

Activity1.java

            Intent intent = new Intent(Activity1.this, Activity2.class);
            intent.putExtra("editTextentered", editText1.getText().toString()); 
            startActivity(intent);
            finish();

Activity2.java

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);
    editText1 = (EditText) findViewById(R.id.editText2);
    String edittextValue = getIntent().getExtras().getString("editTextentered");
    editText1.setText(""+edittextValue);
}
于 2013-08-26T13:18:47.083 回答
1

您需要将这两行切换

editText1 = (EditText) findViewById(R.id.editText2);
setContentView(R.layout.settings);

Views将返回null,直到您layout像使用setContentView(). 所以你onCreate()应该看起来更像

super.onCreate(savedInstanceState);

setContentView(R.layout.settings);  // this needs to be called before instantiating any Views
editText1 = (EditText) findViewById(R.id.editText2);
Bundle extras = getIntent().getExtras(); 

此外,您需要实际获取extra您传递的值,Activity2而您从未这样做过,它将是null. 改变这个

String editTextentered = null;

String editTextentered = extras.getStringExtra("editTextentered");

当您的应用程序崩溃时,logcat 中总会显示一个原因。您应该使用它来帮助您进行调试,如果您无法弄清楚,请在此处发布您的问题,以便我们更轻松地提供帮助。

于 2013-08-26T13:15:42.600 回答
1

当您在设置布局之前获得编辑文本的 id 时,您将因 Null 指针 expetion 而崩溃

  public class Activity1 extends Activity {


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all1);
editText1 = (EditText) findViewById(R.id.editText1);



    }

并在您的第二个活动中执行以下操作

  public class Activity2 extends Activity {

private EditText editText1; 
    protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.settings);
Bundle extras = getIntent().getExtras(); 
String editTextentered = null;
editText1 = (EditText) findViewById(R.id.editText2);
if(extras !=null && extras.containsKey("editTextentered"))
{
   editTextVal= extras.getString("editTextentered");

}
editText1.setText(""+editTextentered);
}

}
于 2013-08-26T13:15:50.150 回答
1

在您的活动 1 中,您正在初始化外部视图onCreate。你会得到NUllPointerException

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all1);
editText1 = (EditText) findViewById(R.id.editText1);

也在活动2

setContentView(R.layout.settings); // must come first
editText1 = (EditText) findViewById(R.id.editText2);
Bundle extras = getIntent().getExtras();  
if(extras !=null )
{
   String editTextVal= extras.getString("editTextentered");
   editText1.setText(editTextVal);

 }

您可以findViewById将当前视图层次结构设置为活动。所以你需要先给activity设置内容,然后再初始化你的views。

于 2013-08-26T13:16:06.667 回答
0

您永远不会在第二个活动中为变量 editTextentered 赋值。替换该行editTextVal= extras.getString("editTextentered");editTextentered= extras.getString("editTextentered");然后在该行周围添加以下代码editText1.setText(""+editTextentered);

if(editTextentered != null) {
    editText1.setText(""+editTextentered);
}
于 2013-08-26T13:17:42.140 回答