Error in Intent

1

I am developing a project in AndroidStudio that consists in consuming certain xml from internet and loading them in CardViews and using RecyclerView. When I click on the image of the Card, I want the article to open in a window within a fragment, but I see an error.

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

I also leave the project URL in GIT to answer any questions. link

Greetings and thank you very much.

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
Context c;
ArrayList<Article> articles;
Activity activity;

public MyAdapter(Context c, ArrayList<Article> articles, Activity activity) {
    this.c = c;
    this.articles = articles;
    this.activity = activity;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(c).inflate(R.layout.model,parent,false);
    return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Article article=articles.get(position);
    String title=article.getTitle();
    //String desc=article.getDescription();
    String dateFecha=article.getTsFecha();
    String dateHora=article.getTsHora();
    String imageUrl=article.getImageUrl();

    holder.titleTxt.setText(title);
    holder.dateFechaTxt.setText(dateFecha);
    holder.dateHoraTxt.setText(dateHora);

    String baseUrl = "http://www.google.cl";//ejemplo
    String cadenaUrl = baseUrl+imageUrl;

   // Log.i("valor total cadena",": "+cadenaUrl);
  Picasso.with(c).load(cadenaUrl).into(holder.img);


    holder.img.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {


            Intent intent = new Intent(activity, PictureDetailActivity.class);
            if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP){

                Explode explode = new Explode();
                explode.setDuration(1000);
                //si estuvieramos en una actividad no sería necesario agregar activity.getWindow, solo getWindow
                //activity.getWindow()

                //como personalizamos la duración de la transición(el objeto Explode), debemos colocar el nombre de la clase instanciada,
                // .setExitTransition(explode);
                // de lo contrario solo sería .setExitTransition(new Explode()); y toma los valores por defecto de Explode.

                activity.getWindow().setExitTransition(explode);
                activity.startActivity(intent, ActivityOptionsCompat.
                        makeSceneTransitionAnimation(activity,v,activity.getString(R.string.transitonname_picture))
                        .toBundle());
            }else {

                activity.startActivity(intent);

            }
        }
    });
}
@Override
public int getItemCount() {
    return articles.size();
}
}
    
asked by Rodrigo 30.05.2017 в 18:02
source

1 answer

1
  

Attempt to invoke virtual method 'java.lang.String   android.content.Context.getPackageName () 'on a null object reference

The problem is that the context you use activity has a null value when you create an Intent :

   Intent intent = new Intent(activity, PictureDetailActivity.class);

In fact you can tell that the activity you are sending is actually sending a null value (third parameter).

  rv.setAdapter(new MyAdapter(c,articles,null));

use the one of the application that is available:

   Intent intent = new Intent(c, PictureDetailActivity.class);
    
answered by 30.05.2017 в 18:37