Reputation: 1
I am creating an application where you would input your height and weight, compute the bmi then click the next button to go to another class and display the bmi result on a fragment within the class.I want to know why it stops after clicking the button. Here is my code:
package com.example.hcon;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity {
Button button1;
EditText et_name;
EditText et_height;
EditText et_weight;
double ht = 0;
double wt = 0;
TextView bmiOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
et_name = (EditText) findViewById(R.id.et_name);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
et_height = (EditText) findViewById(R.id.et_height);
et_weight = (EditText) findViewById(R.id.et_weight);
ht = Double.parseDouble(et_height.getText().toString());
wt = Double.parseDouble(et_weight.getText().toString());
double bmi = wt / (ht * ht);
Intent a = new Intent(FirstActivity.this, SecActivity.class);
a.putExtra(out, out);
Toast.makeText(FirstActivity.this, "Welcome " + et_name.getText().toString(), Toast.LENGTH_LONG).show();
startActivity(new Intent(FirstActivity.this, SecActivity.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
}
SecActivity.class is the activity containing the fragment where the output should be displayed.
Upvotes: 0
Views: 76
Reputation: 484
Use getApplicationContext
and pass the context in intent
. And pass a
in startActivity
Intent a = new Intent(getApplicationContext(), SecActivity.class);
a.putExtra(out, out);
//your toast
startActivity(a));
Upvotes: 1
Reputation: 2446
May be you didn't add your SecActivity to your manifest.
Then it could be easier to find error if you show onCreate
method of SecActivity class.
Upvotes: 0
Reputation: 146
You might be launching your Activity incorrectly. Instead of referring to the class, try getting the current activity when you create your intent. You also want to launch your intent with the startActivity, versus creating a new intent again.
Intent a = new Intent(getActivity(), SecActivity.class);
a.putExtra(out, out);
Toast.makeText(FirstActivity.this, "Welcome "+et_name.getText().toString(), Toast.LENGTH_LONG).show();
startActivity(a);
Upvotes: 0