I have an issue starting a new activity from a CardView Adapter, this is the code:
RVAdapter adapter = new RVAdapter(array_restaurants);
recList.setAdapter(adapter);
And after in the adapter. I set an OnClickListener
personName.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Context context = v.getContext();
System.out.println("Context");
System.out.println(context.toString());
Intent intent = new Intent(v.getContext(), Restaurante.class);
v.getContext().startActivity(intent);
}
});
When i print the context in the console everything looks fine, but after the aplication stop working. Why?
Thank you very much.
pass context of your activity in which you had set adapter
then in your adapter class use this context in place of v.getContext()
and use it like
it may ask to cast context in startActivity with Acitivity you can do that.
Pass the Activity object to your adapter not Context object. I saw your code. Replace this code at these lines MainActivity.java
RVAdapter.java
Hope this solves your problem.
After a lot of time I found an answer, the answer that those people submit help me to found the main problem, this was the solution
In the MainActivity I added a new parameter and pass the activity to the adapter, like @Meenal Sharma and @ch3tan proposed
And in the adaptor
And creating the intent:
That part solve an error but the main error that i had was, when I created a new activity with the Android Studio that created the new activity inheriting of ActionBarActivity, I changed ActionBarActivity for AppCompatActivity and everything worked again...