What I have: I have a RecyclerView with images of bars, coffee shops. etc
What I want: That when you click on one of these images I show you the info of the selected place, but on the moment that you click the selected image the app crashes and shows this error "Attempt to invoke virtual method ' java.lang.String android.content.Context.getPackageName()
' on a null object reference" on the line where i create the Intent. I have already looked for this error but i am doing everything as they say
My question: How do I fix this? Please, I would really appreciate the examples with code, i am not good at programming yet, thanks in advance
My java class
public class foodAndGo extends AppCompatActivity {
ArrayList<ClaseNueva> listalugares;
RecyclerView recyclerview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_and_go);
listalugares = new ArrayList<ClaseNueva>();
recyclerview = (RecyclerView) findViewById(R.id.RecyclerID);
recyclerview.setLayoutManager(new LinearLayoutManager(this));
llenarLugares();
AdapterDatos adapter = new AdapterDatos(foodAndGo.this, listalugares);
recyclerview.setAdapter(adapter);
}
private void llenarLugares() {
listalugares.add(new ClaseNueva("Restaurantes", R.drawable.carnemejor));
listalugares.add(new ClaseNueva("Bares", R.drawable.beers));
listalugares.add(new ClaseNueva("Cafeterías", R.drawable.desayunosmejor));
listalugares.add(new ClaseNueva("Pizzerías", R.drawable.pizzaamejor));
listalugares.add(new ClaseNueva("Favoritos", R.drawable.favoritosmejo));
}
}
My Adapter
public class AdapterDatos extends RecyclerView.Adapter<AdapterDatos.ViewHolder> {
ArrayList<ClaseNueva>listalugares;
Context context;
public AdapterDatos(foodAndGo foodAndGo, ArrayList<ClaseNueva> listalugares) {
this.context = context;
this.listalugares = listalugares;
}
static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {//quizas poner esto en estatic
TextView etiNombre;
ImageView foto;
// Context context;
public ViewHolder(View itemView) {
super(itemView);
// context = itemView.getContext();
etiNombre = (TextView) itemView.findViewById(R.id.idNombre);
foto = (ImageView) itemView.findViewById(R.id.idImagen);
}
void setOnClickListeners(){
foto.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// switch (v.getId()){
// case R.id.idImagen:
// Intent intent = new Intent(context, MapsActivity.class);
// context.startActivity(intent);
// break;
// }
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,null,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.etiNombre.setText(listalugares.get(position).getNombre());
holder.foto.setImageResource(listalugares.get(position).getFoto());
// holder.setOnClickListeners();
if (position == 0){
holder.foto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(context, MapsActivity.class);
context.startActivity(myIntent);
}
});
}
}
@Override
public int getItemCount() {
return listalugares.size();
}
}
I think you forget to pass context from activity !
AdapterDatos adapter = new AdapterDatos(foodAndGo.this,listalugares);
Receive context in Adapter :-
public AdapterDatos(Context context , ArrayList<ClaseNueva> listalugares) {
this.context = context;
this.listalugares = listalugares;
}
I hope this help you!
You need to fetch the context in adapter. I've another solution for. You can get context using the code below
context = holder.foto.getContext();
So, we will get context from foto component in adapter. Lets try!
UPDATE
You need to put that code in onBindViewHolder and above
if (position == 0){
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.