gpt4 book ai didi

java - 尝试将 Intent 转移到另一个适配器时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 09:50:25 27 4
gpt4 key购买 nike

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

我已经尝试在 MainActivity 上使用 Intent

// 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 上进行显式 Activity ,但我无法将 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)

点击图像时应用程序崩溃

最佳答案

你对 RecyclerView 的概念全错了。查看如何使用 RecyclerView Simple RecyclerView Example.

您想要做的是像 Activity 一样启动 RecyclerView 。您的 RecyclerView 不是一个 Activity ,而是一个 View 。在你附加的 gif 中,展示了你想要完成的任务,这是完全不同的事情。单击 RecyclerView 中的项目后,将通过 SharedElement 转换启动一个新 Activity 。您的 DetailAdapter 应该是 AppCompatActivity

要更正您的错误,请让您的 DetailAdapter 扩展 AppCompatActivity,之后,同时使用 Intent pass the data like title, detail through Intent. 启动 DetailAdapter

关于java - 尝试将 Intent 转移到另一个适配器时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56358846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com