【问题标题】:Application Crashes on clicking RecyclerView List Item Holder class单击 RecyclerView List Item Holder 类时应用程序崩溃
【发布时间】:2017-01-15 11:55:27
【问题描述】:

我是 Android 开发的新手。在我的应用程序中,我使用 JSON 从 MySQL 数据库填充 RecyclerView,并创建了多个多个活动。应用程序在我的 PC 上的模拟器上运行顺利,但是当我尝试在我的 Android 手机上运行应用程序时,当我单击 RecyclerView 中的任何项目时它就崩溃了。每个 ListItem 都有多个 TextView 和 ImageView。我无法弄清楚出了什么问题。

根据我的理解,问题区域是:

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);
            }
        });

RecyclerView ListItems 的 Adapter 和 Holder 的完整代码:

    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);
        }
    }
}

我的 ListItem 视图的 XML 是:

<?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>

【问题讨论】:

  • 您可能希望包含来自 logcat 的堆栈跟踪。
  • 您绝对应该遵循 Andrej Jurkins 的建议并发布堆栈跟踪。我要检查的前两个原因是在未初始化的对象上调用方法,或者在主线程上运行持久的操作。我在您的代码中没有看到类似的内容。可能是问题实际上出在CountryDetails
  • @AndrejJurkin 这里是 logcat。它是说 FATAL EXCEPTION: main Process: com.ardentlabs.globalencyc, PID: 17419 android.util.AndroidRuntimeException: 从 Activity 上下文之外调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?
  • 搞定了!!! view.getContext().startActivity(intent);
  • 感谢大家为我查找此内容。 :) 替换: context.startActivity(intent);到:view.getContext().startActivity(intent);

标签: android android-intent android-recyclerview onitemclicklistener onitemclick


【解决方案1】:

您传递给适配器的上下文似乎可能是 ApplicationContext。在这种情况下,将此行添加到您的意图中

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

或者确保上下文是 Activity 类型 (YourActivity.this)。

在适配器类中存储上下文也不是一个好主意,这可能会导致潜在的内存泄漏。如果可能,在绑定视图时使用 view.getContext()。

【讨论】:

    猜你喜欢
    • 2019-09-02
    • 1970-01-01
    • 2015-04-24
    • 2017-09-27
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多