Calling an Activity within a Fragment - Android Studio

1

I need that from the click of a button inside my Fragment, it calls an Activity. My code looks like this:

public class ActFoto2 extends Fragment implements View.OnClickListener, 
ViewTreeObserver.OnGlobalLayoutListener {

    private Button proximo;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

        String caminhoFoto = Util.getUltimaMidia(getActivity(), Util.MIDIA_FOTO);

        if (caminhoFoto != null) {
            mCaminhoFoto = new File(caminhoFoto);
        }

        proximo = (Button)getActivity().findViewById(R.id.btnProximo);

        proximo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ActMapa.class);
                startActivity(intent);
            }
        });
    }
} 

But even so it does not call, and when I go to test, it for the application. Can someone please help me. Thanks.

    
asked by anonymous 01.06.2016 / 20:08

2 answers

0
public class ActFoto2 extends Fragment implements View.OnClickListener, ViewTreeObserver.OnGlobalLayoutListener {

private Button proximo;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    // Define o XML do seu Fragment (o que contem o Button)
    return inflater.inflate(R.layout.meulayout, parent, false);
}

// Qualquer alteração com o Layout XML e Java API deve ser feito aqui ex: Clicks etc.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    proximo = (Button)view.findViewById(R.id.btnProximo);

    proximo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), ActMapa.class);
            startActivity(intent);
        }
    });

}}

That solves it.

    
01.06.2016 / 22:12
-1

Try this:

   Intent intent = new Intent(getActivity(), ActMapa.class);
   getActivity().startActivity(intent);
    
01.06.2016 / 20:25