santafebound santafebound - 3 years ago 106
Android Question

AndroidRuntimeException when clicking URL in Activity

I have an Activity with an Adapter that

extends RecyclerView.Adapter<UserProfileAdapter.ViewHolder>
. The problem is that when a User clicks on an "autolink" in one of the list items, I get the following exception:

In my UserProfileActivity, I instantiate my adapter like his:

UserProfileAdapter adapter = new UserProfileAdapter(getApplicationContext(), posts);


In the adapter, I retrieve the context like this:

private Context mContext;
private List<ParseObject> mYeets;
private UserProfileAdapter adapter;

public UserProfileAdapter(Context context, List<ParseObject> yeets) {
super();

this.mYeets = yeets;
this.mContext = context;
this.adapter = this;
}


enter image description here

How can I make sure that autolinked text does not produce this error? Since there is no code associated with the link/intent, what do I even do?

Answer Source

android.util.AndroidRuntimeException is thrown because in your application is using Application Context to start an activity without the flag FLAG_ACTIVITY_NEW_TASK. Android Runtime does not allow this kind of action because there is no Stack to add this new Activity and the FLAG_ACTIVITY_NEW_TASK flag is important to create a new Stack in your application.

To resolve it, you can pass the Activity context to the Adapter instead of Application Context (i.e.: use this).

e.g.: UserProfileAdapter adapter = new UserProfileAdapter(this, posts);

Good Practice: Always use Activity Context to work with UI elements.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download