The text comes to TextView automatically from VK, it has links, I want to make it so that when I click on this link, it opens in my browser (written by me).

But at transfer of the link from PostAdapter , the error takes off.

 public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> { private LayoutInflater mInflater; private int mShowStyle; VKPostArray obj; VKApiPost p; VKApiPhoto vkPhoto; ArrayList<String> imgUrls1; VKApiUser mainUser; Context ctx; private int lastPosition = -1; public PostAdapter(Context context, VKPostArray posts, VKApiUser user, int showStyle) { super(); this.obj = posts; this.mainUser = user; this.mInflater = LayoutInflater.from(context); this.mShowStyle = showStyle; } @Override public void onBindViewHolder(PostViewHolder holder, int position) { p = obj.get(position); holder.textPost.setText(p.text); holder.textPost.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent15 = new Intent(ctx, Browser.class); intent15.putExtra("link", "LINK"); intent15.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ctx.startActivity(intent15); } }); 

The error is this:

 java.lang.NullPointerException: Attempt to invoke virtual method java.lang.String android.content.Context.getPackageName() on a null object reference at android.content.ComponentName.<init>(ComponentName.java:128) at android.content.Intent.<init>(Intent.java:4449) at calculation.material.oma.sviter.by.PostAdapter$1.onClick(PostAdapter.java:73) at android.view.View.performClick(View.java:5204) at android.view.View$PerformClick.run(View.java:21153) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) 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) 

    1 answer 1

    You have ctx == null apparently.

    Add the following line to the constructor:

     public PostAdapter(Context context, VKPostArray posts, VKApiUser user, int showStyle) { super(); ctx = context; .... }