意图把额外的方法崩溃的应用程序

时间:2016-10-24 18:03:27

标签: android android-intent android-recyclerview onclicklistener

所以这就是我想做的事情, 从Mydata获取图像链接,在图像视图中显示它,然后通过intent put extra方法将图像链接发送到webview活动,并从该图像链接在webview中显示图像。

图像正在图像视图中加载,但每当我尝试通过意图putEtra方法发送image_link时,应用程序就会崩溃onClick方法。

我觉得intent.putExtra("image",my_data.get(position).getImage_link());出了问题 可能是我没有得到图片链接参考

我的数据代码,

package com.techhgeeks.techhgeeks;


public class MyData {

    private int id;
    private String description, image_link;

    public MyData(int id, String description, String image_link) {
        this.id = id;
        this.description = description;
        this.image_link = image_link;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImage_link() {
        return image_link;
    }

    public void setImage_link(String image_link) {
        this.image_link = image_link;
    }
}

适配器代码,

package com.techhgeeks.techhgeeks;
import android.content.Intent;


import android.support.v7.widget.RecyclerView;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import android.view.LayoutInflater;
import java.util.List;
import com.bumptech.glide.Glide;
import android.view.View;
import android.view.ViewGroup;





public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>   {
    private Context context;
    private List<MyData> my_data;

    public MyAdapter(Context context, List<MyData> my_data) {
        this.context = context;
        this.my_data = my_data;
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_child,parent,false);

        final ViewHolder holder = new ViewHolder(itemView);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Intent intent = new Intent(context,article.class);
                intent.putExtra("image",my_data.get(position).getImage_link());
                context.startActivity(intent);

            }
        });

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.description.setText(my_data.get(position).getDescription());
        Glide.with(context).load(my_data.get(position).getImage_link()).into(holder.imageView);



    }

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




    public  class ViewHolder extends  RecyclerView.ViewHolder{

        public TextView description;
        public ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            description = (TextView) itemView.findViewById(R.id.textView);
            imageView = (ImageView) itemView.findViewById(R.id.image);
        }


    }


}

2 个答案:

答案 0 :(得分:0)

您应该在onBindViewHolder方法中设置onClick侦听器。

试试这个:

package com.techhgeeks.techhgeeks;
import android.content.Intent;


import android.support.v7.widget.RecyclerView;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import android.view.LayoutInflater;
import java.util.List;
import com.bumptech.glide.Glide;
import android.view.View;
import android.view.ViewGroup;





public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>   {
    private Context context;
    private List<MyData> my_data;

    public MyAdapter(Context context, List<MyData> my_data) {
        this.context = context;
        this.my_data = my_data;
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_child,parent,false);

        final ViewHolder holder = new ViewHolder(itemView);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.description.setText(my_data.get(position).getDescription());
        Glide.with(context).load(my_data.get(position).getImage_link()).into(holder.imageView);
        holder.view.setOnClickListener(new MyOnItemClickListener(position));
    }

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




    public  class ViewHolder extends  RecyclerView.ViewHolder{

        public TextView description;
        public ImageView imageView;
        public View view;

        public ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            description = (TextView) itemView.findViewById(R.id.textView);
            imageView = (ImageView) itemView.findViewById(R.id.image);
        }


    }

     private class MyOnItemClickListener implements View.OnClickListener {
        private int position;
        public MyOnItemClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context,article.class);
            intent.putExtra("image",my_data.get(position).getImage_link());
                context.startActivity(intent);
        }
    }
}

答案 1 :(得分:0)

尝试将onCLick的代码移动到BindViewHolder并进行一些更改

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    //add this one
    final MyData myData = my_data.get(position);

    //changes my_data.get(position) with myData
    holder.description.setText(myData.getDescription());
    Glide.with(context).load(myData.getImage_link()).into(holder.imageView);

    //move your onClick to here
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context,article.class);
            intent.putExtra("image",myData.getImage_link());
            context.startActivity(intent);
        }
    });
}