Buen dia, por favor su apoyo con este codigo que intento correr pero me arroja error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
package com.example.strongapp.Activities;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.example.strongapp.Activities.Principales.Contenedor;
import com.example.strongapp.R;
import com.example.strongapp.clases.CustomPagerAdapter;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
public class Program extends AppCompatActivity implements View.OnClickListener {
TextView LunesG, MartesG, MiercolesG, JuevesG, ViernesG;
FirebaseFirestore mFire;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_program);
mFire = FirebaseFirestore.getInstance();
ViewPager viewPager = findViewById(R.id.ViewPagerProgram);
viewPager.setAdapter(new CustomPagerAdapter(getApplicationContext()));
LunesG =new TextView(getApplicationContext());
MartesG =new TextView(getApplicationContext());
MiercolesG = new TextView(getApplicationContext());
JuevesG =new TextView(getApplicationContext());
ViernesG = new TextView(getApplicationContext());
VerProgram();
}
@Override
public void onClick(View v) {
}
public void VerProgram() {
mFire.collection("Program").document("Dias").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
String Lunes = documentSnapshot.getString("Lunes");
LunesG = findViewById(R.id.LunesGrande);
LunesG.setText(Lunes);
String Martes = documentSnapshot.getString("Martes");
MartesG = findViewById(R.id.MartesGrande);
MartesG.setText(Martes);
String Miercoles = documentSnapshot.getString("Miercoles");
MiercolesG = findViewById(R.id.MiercolesGrade);
MiercolesG.setText(Miercoles);
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(this, Contenedor.class));
finish();
}
}
Observo que el ID del text view en el xml esta bien escrito:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.Miercoles">
<TextView
android:id="@+id/MiercolesGrade"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048"
android:text="@string/Miercoles"
android:textSize="24sp"
android:textColor="#000000"/>
</androidx.core.widget.NestedScrollView>
Estos Son Los Fragments Anterioes:
El Primero
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.Lunes">
<TextView
android:id="@+id/LunesGrande"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048"
android:text="@string/Lunes"
android:textSize="20sp"
android:textColor="#000000"/>
</androidx.core.widget.NestedScrollView>
El segundo:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.Miercoles">
<TextView
android:id="@+id/MiercolesGrade"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048"
android:text="@string/Miercoles"
android:textSize="24sp"
android:textColor="#000000"/>
</androidx.core.widget.NestedScrollView>
Aqui esta el Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.strongapp">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activities.Skills"/>
<activity android:name=".Activities.Plan" />
<activity android:name=".Activities.Videos" />
<activity android:name=".Activities.Perfil" />
<activity android:name=".Activities.Program" />
<activity
android:name=".Activities.Principales.Splash"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities.Principales.Registro" />
<activity android:name=".Activities.Principales.IniciarSesion"/>
<activity android:name=".Activities.Principales.Contenedor" />
</application>
Aqui esta el ModelObject:
package com.example.strongapp.clases;
import com.example.strongapp.R;
public enum ModelObject {
Lunes(R.string.Lunes, R.layout.fragment_lunes),
Martes(R.string.Martes, R.layout.fragment_martes),
Miercoles(R.string.Miercoles, R.layout.fragment_miercoles);
private int mTitleResId;
private int mLayoutResId;
ModelObject (int titleResId, int layoutResId){
mLayoutResId = layoutResId;
mTitleResId = titleResId;
}
public int getmTitleResId() {
return mTitleResId;
}
public int getmLayoutResId() {
return mLayoutResId;
}
}
Trato de Enviar las variables con los fragments en ViewPager en los Fragments anteriores funciona perfectamente pero cuando intento con el tercero sale ese error.
TextView
en el XML que tenga el idMartesGrande
, por lo que esto está resultando en un NPE:MartesG = findViewById(R.id.MartesGrande);