Reputation: 67
I am just starting with Android development and I have a problem. I am trying to the next thing:
I have an Activity1 that has a button, "0". Also I have Activity2 that has a TextView and an OK button.
When I press on "0", I want to pass some string to Activity2.
When I press on 0, Activity2 has to open, and in it TextView have to show the string that I passed. After pressing on OK (in Activity2), I want to come back to the Activity1.
I tried to do this in this way:
Activity1
case R.id.b0:
errorString = "Error: a number can't start with 0";
Bundle basket = new Bundle();
basket.putString("error", errorString);
Intent person = new Intent("my.firsttraining.app.vitali.ERRORPAGE");
person.putExtras(basket);
startActivity(person);
Activity2
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
bOK= (Button)findViewById(R.id.bOKonError);
errorDisplay = (TextView)findViewById(R.id.tvError);
bOK.setOnClickListener(this);
setContentView(R.layout.errorpage);
Bundle gotBasket = getIntent().getExtras();
String error = gotBasket.getString("error");
errorDisplay.setText(error); ``
I also tried to do something like this:
Activity1
case R.id.b0:
errorString = "Error: a number can't start with 0";
basket = new Bundle();
basket.putString("error", errorString);
person = new Intent("my.firsttraining.app.vitali.ERRORPAGE");
person.putExtras(basket);
setResult(RESULT_OK, person);
finish();
Activity2
bOK.setOnClickListener(this);
setContentView(R.layout.errorpage);
}
public void onClick(View v)
{
Intent in = new Intent("my.firsttraining.app.vitali.MENU");
startActivity(in);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Bundle gotBasket= data.getExtras();
String err = gotBasket.getString("error");
errorDisplay.setText(err);
}
}
After all this, I have an error. After clicking on Button 0, my application crashes. I get the following error.
The application has stopped unexpectedly. Please try again
and the button with text "Force Close".
I have to say that I do not really understand the second option. I just saw it in some tutorial.
Upvotes: 0
Views: 371
Reputation: 18746
In your Activity2.java write setContentView(R.layout.errorpage);
after super.onCreate(savedInstanceState);
so that view can be intit and then you can get button form view.
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Just write this line. You it will load view for
//activity and then you can get button from it
setContentView(R.layout.errorpage);
bOK= (Button)findViewById(R.id.bOKonError);
errorDisplay = (TextView)findViewById(R.id.tvError);
bOK.setOnClickListener(this);
Bundle gotBasket = getIntent().getExtras();
String error = gotBasket.getString("error");
errorDisplay.setText(error);
Upvotes: 1