尝试将意图移至另一个适配器时应用崩溃

时间:2019-05-29 10:52:08

标签: java android android-intent

所以我有两个用于RecyclerView的Adapter类。我希望其中一个Adapter类中的ImageView显示另一个Adapter类

我已经尝试在MainActivity上使用意图

// listener for image
        cardViewHolder.photo.setOnClickListener(new CustomOnItemClick(
                i, new CustomOnItemClick.OnItemClickCallback() {
            @Override
            public void onItemClicked(View view, int position) {
            Intent detail = new Intent(context,DetailAdapter.class);
                context.startActivity(detail);
            }
        }
        ));

这是应用程序应该具有的链接 https://ibb.co/gmnnghw   https://giphy.com/gifs/Y3NK0lHfAcN1dGbCZX

  

CardViewDeviceAdapter

package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

public class CardViewDeviceAdapter extends RecyclerView.Adapter<CardViewDeviceAdapter.CardViewHolder> {

    private Context context;
    private ArrayList<Device> deviceArrayList;
    private ArrayList<Device> getDeviceArrayList() {
        return deviceArrayList;
    }



    public void setDeviceArrayList(ArrayList<Device> deviceArrayList) {
        this.deviceArrayList = deviceArrayList;
    }

    public CardViewDeviceAdapter(Context context) {
        this.context = context;
    }

    @Override
    public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_device, viewGroup, false);
        return new CardViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CardViewHolder cardViewHolder, int i) {
        Device d = getDeviceArrayList().get(i);

        Glide.with(context)
                .load(d.getPhoto())
                .apply(new RequestOptions().override(350, 550))
                .into(cardViewHolder.photo);

        cardViewHolder.title.setText(d.getTitle());
        cardViewHolder.detail.setText(d.getDetail());


        // listener untuk button favorite
        cardViewHolder.favorite.setOnClickListener(new CustomOnItemClick(
                i, new CustomOnItemClick.OnItemClickCallback() {
            @Override
            public void onItemClicked(View view, int position) {
                Toast.makeText(context, "Favorite device"+getDeviceArrayList()
                .get(position)
                .getTitle()
                , Toast.LENGTH_SHORT).show();
            }
        }
        ));

        // listener untuk button share
        cardViewHolder.share.setOnClickListener(new CustomOnItemClick(
                i, new CustomOnItemClick.OnItemClickCallback() {
            @Override
            public void onItemClicked(View view, int position) {
                Toast.makeText(context, "Share device"+getDeviceArrayList()
                                .get(position)
                                .getTitle()
                        , Toast.LENGTH_SHORT).show();
            }
        }
        ));

        // listener untuk image
        cardViewHolder.photo.setOnClickListener(new CustomOnItemClick(
                i, new CustomOnItemClick.OnItemClickCallback() {
            @Override
            public void onItemClicked(View view, int position) {
                Intent detail = new Intent(context, DetailAdapter.class);
                context.startActivity(detail);
            }
        }
        ));
    }

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

    public class CardViewHolder extends RecyclerView.ViewHolder {
        ImageView photo;
        TextView title, detail;
        Button favorite, share;
        public CardViewHolder(View itemView) {
            super(itemView);
            photo = itemView.findViewById(R.id.pc_photo);
            title = itemView.findViewById(R.id.pc_name);
            detail = itemView.findViewById(R.id.pc_details);
            favorite = itemView.findViewById(R.id.pc_favourite);
            share = itemView.findViewById(R.id.pc_share);
        }
    }
}
  

DetailAdapter

package com.example.myapplication;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

public class DetailAdapter extends RecyclerView.Adapter<DetailAdapter.DetailHolder> {

    private Context context;
    private ArrayList<Device> deviceArrayList;
    private ArrayList<Device> getDeviceArrayList() {
        return deviceArrayList;
    }


    @NonNull
    @Override
    public DetailAdapter.DetailHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detail_adapter, viewGroup, false);
        return new DetailAdapter.DetailHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DetailAdapter.DetailHolder detailHolder, int i) {
        Device d = getDeviceArrayList().get(i);

        Glide.with(context)
                .load(d.getPhoto())
                .apply(new RequestOptions().override(350, 550))
                .into(detailHolder.photo);

        detailHolder.title.setText(d.getTitle());
        detailHolder.detail.setText(d.getDetail());
        detailHolder.summary.setText(d.getSummary());
        detailHolder.origin.setText(d.getOrigin());
        detailHolder.market_value.setText(d.getMarket_value());
        detailHolder.consumer.setText(d.getConsumer());
    }

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


    public class DetailHolder extends RecyclerView.ViewHolder {
        ImageView photo;
        TextView title, detail, summary, origin, market_value, consumer;
        public DetailHolder(View view) {
            super(view);
            photo = view.findViewById(R.id.photo_detail);
            title = view.findViewById(R.id.title_title);
            detail = view.findViewById(R.id.detail_title);
            summary = view.findViewById(R.id.detail_detail);
            origin = view.findViewById(R.id.origin_year);
            market_value = view.findViewById(R.id.market_value);
            consumer = view.findViewById(R.id.konsumer);
        }
    }
}

现在,它要求在AndroidManifest.xml上进行明确的活动, 但我无法将DetailAdapter分配给AndroidManifest.xml

Logcat

2019-05-29 17:38:59.097 8897-8897/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 8897
    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.myapplication/com.example.myapplication.DetailAdapter}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2012)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1675)
        at android.app.Activity.startActivityForResult(Activity.java:4586)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
        at android.app.Activity.startActivityForResult(Activity.java:4544)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
        at android.app.Activity.startActivity(Activity.java:4905)
        at android.app.Activity.startActivity(Activity.java:4873)
        at com.example.myapplication.CardViewDeviceAdapter$3.onItemClicked(CardViewDeviceAdapter.java:89)
        at com.example.myapplication.CustomOnItemClick.onClick(CustomOnItemClick.java:16)
        at android.view.View.performClick(View.java:6608)
        at android.view.View.performClickInternal(View.java:6585)
        at android.view.View.access$3100(View.java:782)
        at android.view.View$PerformClick.run(View.java:25945)
        at android.os.Handler.handleCallback(Handler.java:874)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:198)
        at android.app.ActivityThread.main(ActivityThread.java:6729)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

点击图片时应用崩溃

1 个答案:

答案 0 :(得分:1)

您对RecyclerView的所有概念都有误。了解如何从以下位置使用RecyclerView Simple RecyclerView Example.

您要尝试的是像活动一样启动RecyclerView。您的RecyclerView不是活动,它是视图。 在所附的gif中,您想要完成的事情是完全不同的。单击RecyclerView中的项目后,将通过SharedElement过渡启动一个新活动。您的DetailAdapter应该是AppCompatActivity

要更正您的错误,请在执行此操作后,将DetailAdapter扩展AppCompatActivity,同时使用意图pass the data like title, detail through Intent.启动DetailAdapter