0

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.

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Mae
  • 1
  • 2
  • Does it give any errors or crash when you press the button? Or just staying at the current activity with no apparent action taken? – Eric Groves Apr 10 '15 at 03:34
  • It crashes, saying "HCon has stopped." – Mae Apr 10 '15 at 03:44
  • if you want to display the result in a fragment and that too in the FirstActivity, then you probably shouldn't be using Intent or startActivity(). You should be using FragmentManger and Fragment Transaction.You can find useful info [link] (http://developer.android.com/training/basics/fragments/fragment-ui.html#AddAtRuntime) – dora Apr 10 '15 at 04:13
  • Would display the result in the fragment only. – Mae Apr 10 '15 at 04:25
  • Can you please post your activity_first xml and secondActivity or the fragment class code in which you want to post your result. – dora Apr 10 '15 at 04:30
  • public class UserFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { String data = getActivity().getIntent().getExtras().getString("out"); TextView message = (TextView) getView().findViewById(R.id.message); message.setText("Hello, your BMI is ,"+data); return inflater.inflate(R.layout.user_layout, container, false); } } – Mae Apr 10 '15 at 04:43

3 Answers3

1

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));
0

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);
Eric Groves
  • 146
  • 7
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/74954/discussion-on-answer-by-eric-groves-cannot-go-to-the-next-activity). – Taryn Apr 10 '15 at 17:19
0

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.

Community
  • 1
  • 1
Ircover
  • 2,406
  • 2
  • 22
  • 42