I have a static and inner class inside my Activityclass
. I need to call an Activity using Intent(context,nameOfTheNewActivity.class)
because I am trying to call an Intent
when an item from a RecyclerView.ViewHolder
is clicked. I had to Override the onClick
to get the position of the item was clicked using getLayoutPosition()
(this getLayoutPosition() worked fine).
Now when I try to use Intent I have the error:
Non-static method cannot be referenced by a static context.
I read another links from Stackoverflow like this. How do I call an Intent in this case of static context and inside an inner class, I.e, how do I get the context inside an inner class, and how do I solve the **fundamental ** error to do not call a non static class from an static class?
I tried the following before ask here:
Get the context from the View using v.context but I continue with the problem - and still calling a non static method from a static context.
Delete the word static form my inner class, but did not solve and the app crashes.
My code:
public class ActivityOne extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener {
public static class MessageViewHolderOfFriend extends RecyclerView.ViewHolder { public MessageViewHolderOfFriend(View v) {
super(v);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
;
Intent intent = new Intent(v.getContext(),NewActivityToRun.class);
startActivity(intent);//Error in this Line//
}
});
}
}