Matosh
Matosh

Reputation: 33

Trying to open activity from button press in fragment. Nothing happens

Tried to open a fragment from a button in a fragment. Wasnt able to get this to work so decided just to make it an activity. Tried doing this but still am not able to open the activity on the button press.

public class ZonnepaneelLayout extends Fragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.zonnepaneel_layout, container, false);
        Button button2 = (Button)view.findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getActivity(), AddZonnepaneel.class);
                startActivity(i);
            }
            });

        getActivity().setTitle("Zonnepaneel");
        return view;
    }
}

Activity

public class AddZonnepaneel extends AppCompatActivity {
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.add_zonnepaneel_layout);
      }
}

Upvotes: 0

Views: 109

Answers (2)

Yasiru Nayanajith
Yasiru Nayanajith

Reputation: 1737

Try using the getContext() instead of the getActivity()

Intent intent = new Intent(getContext(), AddZonnepaneel.class);
startActivity(i);

Upvotes: 2

isidor99
isidor99

Reputation: 72

First of all, your variable View can't start with uppercase letter, set it to lowercase :)

Try to use

getActivity().startActivity(i);
getActivity().finish();

Upvotes: -1

Related Questions