Search code examples
androidandroid-intentandroid-recyclerviewonitemclicklisteneronitemclick

Application Crashes on clicking RecyclerView List Item Holder class


I am new to Android development. In my app, I was populating RecyclerView from MySQL database using JSON, and created multiple multiple Activities. Application is running smoothly on Emulator on my PC, but when I try running app on my Android phone, it just crashed when I click on any Item in RecyclerView. Every ListItem has multiple TextView and ImageView. I am unable to figure out what is wrong.

The Problem area as per my understating is:

holder.llCountryListItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Toast.makeText(context, "You clicked on " + countId, Toast.LENGTH_SHORT).show();
                Toast.makeText(context, "You clicked on " + context, Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(context, CountryDetails.class);

                intent.putExtra("id", countId);
                intent.putExtra("name", countryListItem.getName());
                intent.putExtra("capital", countryListItem.getCapital());
                intent.putExtra("continent", countryListItem.getContinent());
                intent.putExtra("population", countryListItem.getPopulation());

                context.startActivity(intent);
            }
        });

Complete Code for Adapter and Holder for RecyclerView ListItems:

    public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.ViewHolder> {

    private List<CountryListItem> countryListItems;
    private Context context;

    public CountryAdapter(List<CountryListItem> countryListItems, Context context) {
        this.countryListItems = countryListItems;
        this.context = context;
    }

    @Override
    public CountryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.country_list_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CountryAdapter.ViewHolder holder, int position) {
        final CountryListItem countryListItem = countryListItems.get(position);

        holder.textViewName.setText(countryListItem.getName());
        holder.textViewCapital.setText(countryListItem.getCapital());
        holder.textViewId.setText(countryListItem.getContinent() + "\nPopulation: " + countryListItem.getPopulation() + " - GDP: " + countryListItem.getGdp());

        final int countId;

        countId = (Integer) countryListItem.getId();

        Picasso.with(context)
                .load(imageURL)
                .into(holder.imCountry);

        holder.llCountryListItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(context, "You clicked on " + context, Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(context, CountryDetails.class);

                intent.putExtra("id", countId);
                intent.putExtra("name", countryListItem.getName());
                intent.putExtra("capital", countryListItem.getCapital());
                intent.putExtra("continent", countryListItem.getContinent());
                intent.putExtra("population", countryListItem.getPopulation());

                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return countryListItems.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView textViewId;
        public TextView textViewName;
        public TextView textViewCapital;

        public ImageView imCountry;

        public LinearLayout llCountryListItem;

        public ViewHolder(View itemView) {
            super(itemView);

            textViewId = (TextView) itemView.findViewById(R.id.tvId);
            textViewName = (TextView) itemView.findViewById(R.id.tvName);
            textViewCapital = (TextView) itemView.findViewById(R.id.tvCapital);
            imCountry = (ImageView) itemView.findViewById(R.id.imCountry);

            llCountryListItem = (LinearLayout) itemView.findViewById(R.id.llCountryListItem);
        }
    }
}

and XML for my ListItem view is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dp" >

    <android.support.v7.widget.CardView
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/llCountryListItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@android:color/holo_blue_dark"
            android:padding="1dp">

            <TextView
                android:id="@+id/tvName"
                android:text="Name"
                android:textSize="16sp"
                android:textStyle="bold"
                android:textColor="#ffffff"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="2dp"
                android:background="@android:color/holo_blue_light">

                <ImageView
                    android:id="@+id/imCountry"
                    android:layout_gravity="center"
                    android:layout_width="70dp"
                    android:layout_height="45dp" />

                <LinearLayout
                    android:paddingLeft="@dimen/activity_horizontal_margin"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/tvCapital"
                        android:textSize="14sp"
                        android:textStyle="bold"
                        android:textColor="#efefef"
                        android:text="Capital"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/tvId"
                        android:text="Asia, Population: 185,963,247"
                        android:textSize="12sp"
                        android:textColor="#fafafa"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                </LinearLayout>

            </LinearLayout>
        </LinearLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

Solution

  • It seems that context you are passing to your adapter could be ApplicationContext. In this case add this line to your intent

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    

    Or make sure context is of Activity type (YourActivity.this).

    Also storing a Context inside your adapter class is not a good idea, this could lead to potential memory leaks. Use view.getContext() when binding your views if possible.