在recyclerView适配器的imageview上的意图

时间:2017-07-12 20:58:01

标签: android android-intent android-recyclerview

我在RecyclerView中遇到了关于意图的问题。 我只想在我的RecyclerView中使用1个imageView。

错误是:

  

android.util.AndroidRuntimeException:从中调用startActivity()   在Activity上下文之外需要FLAG_ACTIVITY_NEW_TASK   旗。这真的是你想要的吗?                                                                         在android.app.ContextImpl.startActivity(ContextImpl.java:1383)                                                                         在android.app.ContextImpl.startActivity(ContextImpl.java:1370)                                                                         在   android.content.ContextWrapper.startActivity(ContextWrapper.java:323)                                                                         在example.memories.MyAdapter $ ViewHolder $ 1.onClick(MyAdapter.java:75)                                                                         在android.view.View.performClick(View.java:4871)                                                                         在android.view.View $ PerformClick.run(View.java:20365)                                                                         在android.os.Handler.handleCallback(Handler.java:815)                                                                         在android.os.Handler.dispatchMessage(Handler.java:104)                                                                         在android.os.Looper.loop(Looper.java:214)                                                                         在android.app.ActivityThread.main(ActivityThread.java:6102)                                                                         at java.lang.reflect.Method.invoke(Native Method)                                                                         在java.lang.reflect.Method.invoke(Method.java:372)                                                                         在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1028)                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)07-13   01:55:31.033 20699-29708 / example.memories E / NativeCrypto:   ssl = 0x5578a74560 cert_verify_callback x509_store_ctx = 0x7f9844d228   arg = 0x0 07-13 01:55:31.033 20699-29708 / example.memories   E / NativeCrypto:ssl = 0x5578a74560 cert_verify_callback调用   verifyCertificateChain authMethod = ECDHE_RSA

我的RecyclerView适配器代码是:

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

    private Context context;
    private List<Upload> uploads;
    Upload upload;

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

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
         upload = uploads.get(position);

       holder.textViewName.setText(upload.getName());

        Glide.with(context).load(upload.getUrl()).into(holder.imageView);
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView textViewName;
        public ImageView imageView,shareImage;


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

           textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            shareImage=(ImageView)itemView.findViewById(R.id.share);

            shareImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(upload.getUrl()));
                    shareIntent.setType("image/*");
                   context.startActivity(Intent.createChooser(shareIntent, "Share memories:"));

                }
            });
        }

    }

请指导我。我怎么能实现这个目标?

2 个答案:

答案 0 :(得分:0)

在构造MyAdapter时传入Activity上下文而不是Application上下文,或在调用context.startActivity之前添加以下行:

{
  "apiVersion": "2015-01-01",
  "type": "Microsoft.Resources/deployments",
  "name": "[concat('DeployRole',copyIndex(1) ,variables('vmRole1Name'))]",
  "copy": {
    "name": "rolecopy",
    "count": 2
  },
  "properties": {
    "mode": "Incremental",
    "templateLink": {
      "uri": "<proper URL here>",
      "contentVersion": "1.0.0.0"
    },
    "parameters": {
      ...
      "roleInstanceCount": { "value": "[concat('variables(', variables('singleQuote'), 'vmRole', copyIndex(1), variables('singlequote'), ').roleInstanceCount')]" },
      ...

答案 1 :(得分:0)

不要将getApplicationContext()传递给您的适配器。只需通过您的活动背景,您甚至不需要shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);这样做。

使用应用程序上下文时要小心。 我真的建议你阅读Difference between getContext() , getApplicationContext() , getBaseContext() and "this"