Dhafer Hn Dhafer Hn - 2 months ago 6
Android Question

Intent : To start an activity, unable to start the second activity

The application aims to write a message at the first activity, and after clicking on a button, this message appears in the second activity.
But After installing the application on smartphone, when i click on the button, a message appears saying " application is stopped". Help me please." i USED ECLIPSE for developping this application"

The first activity code:

public class MainActivity extends Activity {

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

public void login (View view) {


EditText user = (EditText) findViewById(R.id.user);
EditText password = (EditText) findViewById(R.id.password);
Intent myintent= new Intent (getApplicationContext(),Main2Activity.class); //permet de basculer de cette interface à la 2éme aprés avoir éxécuter "startActivity(myintent)";

Bundle b=new Bundle();
b.putString("username", user.getText().toString());
b.putString("password", password.getText().toString());
myintent.putExtras(b);
startActivity(myintent);
}


The second activity:

public class Main2Activity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView mes=(TextView)findViewById(R.id.mes);

Bundle b=getIntent().getExtras();
string user=(string) b.get("username");
string pass=(string) b.get("password");
mes.setText("user:"+user +"password:"+pass);

}

Answer

Try a simpler way to pass data as a String,without using bundle.

MainActivity.java

Intent i= new Intent(MainActivity.this,Main2Activity .class);
i.putExtra("username",user.getText().toString());
i.putExtra("password",password.getText().toString());
startActivity(i);

and in Main2Activity .java

String name=getIntent().getExtras().getString("username");
txtView.setText(name);
String pass=getIntent().getExtras().getString("password");
txtView2.setText(pass);