3

El problema es que tengo un Fragment y no funciona... :

He probado esto pero no funciona :

public class FragmentoPerfil extends Fragment {


    CalendarView calendar;
    public FragmentoPerfil() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        calendar = (CalendarView) getActivity().findViewById(R.id.calendarView);
        calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                // display the selected date by using a toast
                Toast.makeText(getActivity().getApplicationContext(), dayOfMonth + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
            }
        });
        return inflater.inflate(R.layout.fragmento_perfil, container, false);
    }


}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dee4ee"
    android:orientation="vertical"
    android:padding="@dimen/espacio_norma_1"
    android:weightSum="1">

    <CalendarView
        android:layout_width="match_parent"
        android:layout_height="371dp"
        android:id="@+id/calendarView"
        android:layout_weight="0.40" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/float_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</LinearLayout>

Parte donde llamo a los Fragment en el MainActivity:

private void seleccionarItem (MenuItem itemDrawer)
{

        Fragment fragmentoGenerico = null;
        FragmentManager fragmentManager = getSupportFragmentManager();

        switch (itemDrawer.getItemId()) {

            case R.id.CALENDARIO:

                fragmentoGenerico = new FragmentoPerfil();


                break;

            case R.id.PRIMERO:
                fragmentoGenerico = new FragmentPrimero();
                break;

            case R.id.SEGUNDO:
                fragmentoGenerico = new FragmentSegundo();
                break;

            case R.id.TERCERO:
                fragmentoGenerico = new FragmentTercero();
                break;

            case R.id.CUARTO:
                fragmentoGenerico = new FragmentCuarto();
                break;

            case R.id.nav_share:
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "AndroidSolved");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "si que funciona");
                startActivity(Intent.createChooser(sharingIntent, "Share via"));
                break;
    }
2
  • No creo que se pueda considerar duplicado ya que esta pregunta hace referencia a un Fragment y la otra a un Activity.
    – Jorgesys
    el 29 ago. 2016 a las 14:59
  • Gracias Elena :)
    – Rf Mvs
    el 29 ago. 2016 a las 17:37

1 respuesta 1

0

Asegura que en el layout que carga la Activity en realidad se encuentre el CalendarView ya que haces referencia a este en el layout de la Activity no en el Fragment:

 calendar = (CalendarView) getActivity().findViewById(R.id.calendarView);

Para mostrar el Toast debe ser suficiente usar el contexto de la Activity:

 Toast.makeText(getActivity(), dayOfMonth + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
3
  • El problema es que no está ahi el calendarView... pero no puedo ponerlo donde realmente está... :S tendré que indagar, gracias.
    – Rf Mvs
    el 29 ago. 2016 a las 17:38
  • Por cierto Elena, me queda una duda gigante en mi proyecto, estas cosas las puedo intentar hacer pero... hay una que me supera y ya la planteé como pregunta también... tengo un arraylist que se muestra en un fragment pero necesito que al hacer click en uno se vaya a una activity pero sin "machacar" que ya la respondiste creo pero... no se me solucionó... :S
    – Rf Mvs
    el 29 ago. 2016 a las 17:41
  • Entonces por eso no se puede mostrar el Toast. El calendarView lo puedes agregar en el layout del Fragment o del Activity.
    – Jorgesys
    el 29 ago. 2016 a las 17:48

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.