Skip to main content
3 de 3
oncreate
lujan
  • 129
  • 1
  • 12

Error al pasar el codigo de una activity a un fragment (recyclerview)

Quiero pasar el codigo que tengo de una activity a unos tabs y me da error

NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

me da fallo en la linea que esta dentro del onCreateView

rv = vista.findViewById( R.id.recycler );

rv.setLayoutManager( new LinearLayoutManager( getActivity() ) );

Por donde pueden ir los tiros? que codigo puedo mostrar mas para que me puedan ayudar? no se si esto servira pero las importanciones de los fragmnets los hago con android.support.v4.app.Fragment;

xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_VoteAct"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lujan.confirmaciones.Views.FinalActivity">

<TextView
    android:id="@+id/tvTitulo"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:maxLength="25"
    android:text="@string/title"
    android:textAlignment="center"
    android:textColor="@color/ColorActionBarTitle"
    android:textSize="28sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tvDescripcion"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="18dp"
    android:layout_marginTop="8dp"
    android:maxLength="70"
    android:text="@string/description"
    android:textAlignment="center"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView2" />

<TextView
    android:id="@+id/tvCodigo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="28dp"
    android:layout_marginTop="4dp"
    android:onClick="shareCode"
    android:text="@string/code"
    android:textAlignment="center"
    android:textColor="#00bcd4"
    app:layout_constraintBottom_toTopOf="@+id/tv_diasrestantes"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tv_contador"
    app:layout_constraintVertical_bias="0.0"
    tools:ignore="OnClick" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="330dp"
    android:layout_height="102dp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvTitulo"
    app:srcCompat="@android:drawable/sym_def_app_icon" />

<Button
    android:id="@+id/btnYes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginStart="36dp"
    android:elevation="5dp"
    android:onClick="yes"
    android:text="@string/Yes"
    android:textColor="@android:color/holo_green_dark"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:ignore="OnClick" />

<Button
    android:id="@+id/btnNo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:elevation="5dp"
    android:onClick="no"
    android:text="@string/No"
    android:textColor="@android:color/holo_red_light"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    tools:ignore="OnClick" />

<Button
    android:id="@+id/btnSoSo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginEnd="36dp"
    android:elevation="5dp"
    android:onClick="maybe"
    android:text="@string/Maybe"
    android:textColor="@android:color/holo_blue_bright"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    tools:ignore="OnClick" />

<TextView
    android:id="@+id/tv_contador"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"

    android:layout_marginTop="8dp"
    android:text="@string/share_code"
    android:textSize="14sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/recycler" />

<TextView
    android:id="@+id/tv_diasrestantes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="TextView"
    android:textSize="16sp"
    app:layout_constraintBottom_toTopOf="@+id/btnNo"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler"
    android:layout_width="0dp"
    android:layout_height="301dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="12dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvDescripcion"
    tools:ignore="MissingConstraints">

</android.support.v7.widget.RecyclerView>

<TextView
    android:id="@+id/tv.calendar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/add_to_calendary"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/recycler" />

<Button
    android:id="@+id/bt.calendar"
    android:layout_width="35dp"
    android:layout_height="27dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="4dp"
    android:background="@drawable/agendas"
    android:onClick="calendario"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tv.calendar"
    tools:ignore="OnClick" />

<Button
    android:id="@+id/btn_DeleteEvent"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/ColorActionBarTitle"
    android:onClick="deleteEvent"
    android:text="@string/Delete_evento"
    android:textColor="@android:color/background_dark"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:ignore="OnClick" />

</android.support.constraint.ConstraintLayout>

Codigo Fragment

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

    View vista = inflater.inflate( R.layout.tab3_fragment, container, false );

   // loadLanguage();
   // setContentView( R.layout.activity_final );
   // return new FinalActivity(this);

    // UI
    layout = vista.findViewById( R.id.layout_VoteAct );
    mTitle = vista.findViewById( R.id.tvTitulo );
    mDescription = vista.findViewById( R.id.tvDescripcion );
    mCode = vista.findViewById( R.id.tvCodigo );
    mImage = vista.findViewById( R.id.imageView2 );
    Si = vista.findViewById( R.id.btnYes );
    No = vista.findViewById( R.id.btnNo );
    Nose = vista.findViewById( R.id.btnSoSo );
    Delete = vista.findViewById( R.id.btn_DeleteEvent );
    compartir = vista.findViewById( R.id.tv_contador );
    calendario = vista.findViewById( R.id.tv_calendar );
    calendar = vista.findViewById( R.id.bt_calendar );
    //prueba=findViewById( R.id.tv_prueba );
    rv = vista.findViewById( R.id.recycler );
    //cont=findViewById( R.id.tv_contador );
    dias = vista.findViewById( R.id.tv_diasrestantes );


    // Intent
    code = getActivity().getIntent().getExtras().getString("code");
    dataEventIntent = getActivity().getIntent().getExtras().getString("data");
    guri = getActivity().getIntent().getExtras().getString("uri");
    random = getActivity().getIntent().getExtras().getString("random");
   // Intent intent = getIntent();
    /*
    code = intent.getStringExtra( "code" );
    dataEventIntent=intent.getStringExtra( "data" );
    guri=intent.getStringExtra( "uri" );
    random=intent.getStringExtra( "random" );
    */

    // Habilitar el modo OffLine de la BBDD
    if (FirebaseApp.getApps( getActivity() ).isEmpty()) {
        FirebaseApp.initializeApp( getActivity() );
        FirebaseDatabase.getInstance().setPersistenceEnabled( true );
    }

    mDatabase = FirebaseDatabase.getInstance().getReference().child( "Events" ).child( code );

    mDatabase.addValueEventListener( new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Event event = dataSnapshot.getValue( Event.class );
            mTitle.setText( event.getTitle() );
            mDescription.setText( event.getDescription() );
            //  prueba.setText( event.getDateEvent() );
            dataEvent=event.getDateEvent();
            code=event.getCode();
            url=event.getFoto();
            mCode.setText(event.getCode());
            ano=event.getAno();
            mes=event.getMes();
            dia=event.getDia();
            countDate();//diferencia fechas
            url();//foto evento
        }

        @Override
        public void onCancelled(final DatabaseError databaseError) {
        }
    } );

    user = FirebaseAuth.getInstance().getCurrentUser();

    //RecyclerView
    FirebaseDatabase database=FirebaseDatabase.getInstance();
    usuarios=new ArrayList<>( );

    rv.setLayoutManager( new LinearLayoutManager( getActivity() ) );  //FALLO RV RECIBE NULL
    adapter=new Adapter( usuarios );
    rv.setAdapter( adapter );


    database.getReference("Events").child( code ).child( "Users" ).addValueEventListener( new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            usuarios.removeAll( usuarios );
            for (DataSnapshot snapshot:dataSnapshot.getChildren()) {
                User user=snapshot.getValue(User.class) ;
                usuarios.add( user );

            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    } );//cada vez que se cambien datos se refresca


    countVote();

    return vista;
}
lujan
  • 129
  • 1
  • 12