无法将数据从一个意图传递到另一个意图

时间:2017-07-18 11:06:40

标签: android-intent

我试图将一些数据从一个意图传递到另一个意图。但是当我在第一个意图上放置值后按下按钮时,在Android工作室运行此代码时,它显示“意图已停止。

**我想将数据从activity-1传递给activity-2

活性-1:

 public class MainActivity extends AppCompatActivity{


EditText edit1;
EditText edit2;
Button btn;

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

    edit1= (EditText) findViewById(R.id.edit1);
    btn= (Button) findViewById(R.id.button);
    edit2= (EditText) findViewById(R.id.edit2);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                Intent i = new Intent(MainActivity.this, Main2Activity.class);

                i.putExtra("username", edit1.getText().toString());
                i.putExtra("password", edit2.getText().toString());
                startActivity(i);

        }
    });
}

}

活动-2

public class Main2Activity extends AppCompatActivity {
TextView txt1;
TextView txt2;

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

    txt1=(TextView)findViewById(R.id.edit3);
    txt1=(TextView)findViewById(R.id.edit4);

    Intent i2=this.getIntent();
String message=i2.getStringExtra("username");

    String message2=i2.getStringExtra("password");

    txt1.setText(message);
    txt2.setText(message2);



}

}

1 个答案:

答案 0 :(得分:0)

您尝试使用此代码。

活性-1:

    public class MainActivity extends Activity {

        EditText edit1, edit2;
        Button btn;

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

            edit1= (EditText) findViewById(R.id.edit1);             
            edit2= (EditText) findViewById(R.id.edit2);
            btn= (Button) findViewById(R.id.button);

            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                        Intent intent = new Intent(MainActivity.this, Main2Activity.class);

                        String val1 = edit1.getText().toString();
                        String val2 = edit2.getText().toString();
                        intent.putExtra("username", val1);
                        intent.putExtra("password", val2);
                        startActivity(intent);          
                }
            });
        }
    }

活性-2:

    public class Main2Activity extends Activity {
        TextView txt1;
        TextView txt2;

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

            txt1 = (TextView)findViewById(R.id.edit3);
            txt1 = (TextView)findViewById(R.id.edit4);

            String username = getIntent().getStringExtra("username");
            String password = getIntent().getStringExtra("password");

            txt1.setText(message);
            txt2.setText(password);         
        }
    }