When trying to set text in a TextView in the following way:
Class - > FavouritePet.java
public class FavouritePet extends AppCompatActivity {
private ArrayList<Pet> pets;
private RecyclerView rvPets2;
private TextView numeroLikes, nombreMascota;
private ImageView foto;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.favourite_pet);
//Establecemos la Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.myActionBar);
setSupportActionBar(toolbar);
//Para el boton de back
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
rvPets2 = (RecyclerView) findViewById(R.id.rvPets2);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
rvPets2.setLayoutManager(llm);
inicializarAdaptador();
pets = new ArrayList<Pet>();
ordenarMascotas(pets);
//Bundle
Bundle bndl=getIntent().getExtras();
Pet pet = inicializarMascotas(bndl);
String nombreMascota = pet.getPetName();
int numeroLikes = pet.getNumberOfLikes();
int foto = pet.getFoto();
this.nombreMascota = (TextView) findViewById(R.id.tvPetNameFavourite);
this.numeroLikes = (TextView) findViewById(R.id.tvLikesNumberFavourite);
this.foto = (ImageView) findViewById(R.id.ivFavouritePetImage);
//NullPointerException -> Estoy tratando de settear un texto en un objeto nulo.
this.nombreMascota.setText(nombreMascota);
this.numeroLikes.setText(numeroLikes);
this.foto.setImageResource(foto);
}
public ArrayList<Pet> ordenarMascotas(ArrayList<Pet> pets){
Collections.sort(pets, new Comparator() {
@Override
public int compare(Object o, Object t1) {
return 0;
}
public int compare(Pet p1, Pet p2) {
return new Integer(p2.getNumberOfLikes()).compareTo(new Integer(p1.getNumberOfLikes()));
}
});
return pets;
}
public Pet inicializarMascotas(Bundle bndl){
String nombreMascota = bndl.getString(String.valueOf(R.string.nombre_mascota));
int numeroLikes = bndl.getInt(String.valueOf(R.string.numberOfLikes));
int foto = bndl.getInt(String.valueOf(R.string.foto));
Pet p1 = new Pet (numeroLikes,nombreMascota,foto);
pets.add(p1);
return p1;
}
public void inicializarAdaptador(){
FavouriteAdapter fva = new FavouriteAdapter(pets,this);
rvPets2.setAdapter(fva);
}
}
The identifiers are correctly written in xml.
Layout - > cardview_favourite_pets.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="@dimen/cardCornerRadius"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include
android:id="@+id/myActionBar"
layout="@layout/actionbar"
>
</include>
<ImageView
android:id="@+id/ivFavouritePetImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_left1"
android:src="@drawable/perro"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/ivEmptyBoneFavourite"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/dog_bone_48"
android:layout_marginLeft="@dimen/margin_left3"
/>
<TextView
android:id="@+id/tvPetNameFavourite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_left1"
android:text="@string/nombre_mascota"
android:textStyle="bold"
android:textSize="@dimen/textSize"
/>
<TextView
android:id="@+id/tvLikesNumberFavourite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_left2"
android:textSize="@dimen/textSize"
android:textStyle="bold"
android:text="@string/numberOfLikes"
/>
<ImageView
android:id="@+id/ivFullBoneFavourite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dog_bone_48_1"
android:layout_marginLeft="@dimen/margin_left3"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
I think the error should be somewhere in the xml because I do not see any error in the Java code. Looking in the xml code, I realized that in android:context
was assigned the Layout .MainActivity
, when in fact I had to assign the corresponding layout, in my case .FavouritePet
(Copy-paste things hehe). I leave the xml code to see if someone can help me out.
Layout - > favourite_pet.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="es.uclm.mylittlepets.FavouritePet">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvPets2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
The error he gives me is this:
08-22 09:10:52.142 2716-2716/es.uclm.mylittlepets E/AndroidRuntime: FATAL EXCEPTION: main
Process: es.uclm.mylittlepets, PID: 2716
java.lang.RuntimeException: Unable to start activity ComponentInfo{es.uclm.mylittlepets/es.uclm.mylittlepets.FavouritePet}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at es.uclm.mylittlepets.FavouritePet.onCreate(FavouritePet.java:59)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
EDIT:
Here I leave the debugging result to the method inicializarMascotas(bndl)
The Pet class is:
public class Pet {
//Atributos
private int numberOfLikes, foto;
private String petName;
//Constructor
public Pet (int numberOfLines, String petName, int foto){
this.numberOfLikes=numberOfLines;
this.petName=petName;
this.foto = foto;
}
public int getNumberOfLikes() {
return numberOfLikes;
}
public void setNumberOfLikes(int numberOfLines) {
this.numberOfLikes = numberOfLines;
}
public String getPetName() {
return petName;
}
public void setPetName(String petName) {
this.petName = petName;
}
public int getFoto(){
return foto;
}
public void setFoto (int foto) {
this.foto = foto;
}
I think there is no problem executing the getPetName()
method
I add the adapter class - > FavouriteAdapter.java
public class FavouriteAdapter extends RecyclerView.Adapter<FavouriteAdapter.FavouriteViewHolder>{
ArrayList<Pet> pets;
Activity activity;
public FavouriteAdapter(ArrayList<Pet> pets, Activity activity){
this.pets=pets;
this.activity = activity;
}
@Override
public FavouriteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_favourite_pets,parent,false);
return new FavouriteViewHolder(v);
}
@Override
public void onBindViewHolder(FavouriteViewHolder holder, int position) {
Pet pet = pets.get(position);
asignarAtributos(holder,pet);
}
@Override
public int getItemCount() {
return pets.size();
}
public void asignarAtributos(FavouriteViewHolder holder, Pet pet){
holder.petName.setText(pet.getPetName());
holder.numberOfLikes.setText(String.valueOf(pet.getNumberOfLikes()));
holder.ibStar.setImageResource(R.drawable.star_48);
holder.ivCardview.setImageResource(pet.getFoto());
holder.ivFullBone.setImageResource(R.drawable.dog_bone_48_1);
holder.ivEmptyBone.setImageResource(R.drawable.dog_bone_48);
}
public static class FavouriteViewHolder extends RecyclerView.ViewHolder{
//Atributos
private TextView numberOfLikes, petName;
private ImageButton ibStar;
private ImageView ivCardview, ivFullBone, ivEmptyBone;
public FavouriteViewHolder(View itemView) {
super(itemView);
asociarElementos(itemView);
}
public void asociarElementos(View itemView){
numberOfLikes = (TextView) itemView.findViewById(R.id.tvLikesNumberFavourite);
petName = (TextView) itemView.findViewById(R.id.tvPetNameFavourite);
ibStar = (ImageButton) itemView.findViewById(R.id.ibStar);
ivCardview = (ImageView) itemView.findViewById(R.id.ivFavouritePetImage);
ivFullBone = (ImageView) itemView.findViewById(R.id.ivFullBoneFavourite);
ivEmptyBone = (ImageView) itemView.findViewById(R.id.ivEmptyBoneFavourite);
}
}
}