0

I have this fragment activity called BmiFragment from Navigation Drawer using Sliding Menu, from which i want to go to a new activity i.e. BmiCalculator.class to perform some task but i am not able to do so. I am trying to do this by implementing onclicklistener to the fragment activity. In the XML layout there is simply four buttons by clicking on them i want to open a new activity to perform certain task. Please provide me some help. Click here to view for errors

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class BmiFragment extends Fragment implements OnClickListener {

    public BmiFragment() {
    }

    Button btn, btn1, btn2, btn3;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_bmi, container,
                false);
        btn = (Button) rootView.findViewById(R.id.button1);
        btn.setOnClickListener(this);
        btn1 = (Button) rootView.findViewById(R.id.button2);
        btn1.setOnClickListener(this);
        btn2 = (Button) rootView.findViewById(R.id.button3);
        btn2.setOnClickListener(this);
        btn3 = (Button) rootView.findViewById(R.id.button4);
        btn3.setOnClickListener(this);
        return rootView;

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.button1:
        Intent intent = new Intent(this, BmiCalculator.class);//***ERROR: The constructor Intent(BmiFragment, Class<BmiCalculator>) is undefined***//
        startActivity(intent);[enter image description here][1]
        break;

    case R.id.button2:

        break;

    case R.id.button3:

        break;
    case R.id.button4:

        break;
    default:
        break;
    }
}

}

  • Hanni, have a look at [this answer](http://stackoverflow.com/questions/12074608/how-do-i-start-an-activity-from-within-a-fragment). – miha May 04 '16 at 20:52

1 Answers1

0

Intent intent = new Intent(this, BmiCalculator.class);//ERROR: The constructor Intent(BmiFragment, Class) is undefined// startActivity(intent);[enter image description here]

instead of this use

Intent intent = new Intent(getActivity(), BmiCalculator.class);
startActivity(intent);
TOP_GUN007
  • 53
  • 1
  • 1
  • 6