я проверял трижды, я уверен, что все идентификаторы верны, но все же есть какая-то ошибка
Я пытаюсь получить заголовок, изображение и описание для второго действия, но там написано
void android .widget.ImageView.setImageResource (int) 'для нулевой ссылки на объект
Я могу получить заголовок, изображение и описание для первого действия, но когда я пытаюсь получить в другом он говорит о нулевом объекте :(
Может кто-нибудь помочь мне, пожалуйста, что не так, я делаю здесь
activity_main. xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/reycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
Animal_acctivity. xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AnimalActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/animal_item_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
animal_item. xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:paddingTop="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="8dp"
android:background="@android:color/black"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/animallayout"
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/animal_logo"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/animals" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/animal_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Story Title"
android:textColor="@android:color/widget_edittext_dark"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/animal_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Story Description"
android:textColor="@android:color/widget_edittext_dark"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
book_activity. xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:paddingTop="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="8dp"
android:background="@android:color/black"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/book_logo_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/animals" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/story_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Story Title"
android:textColor="@android:color/widget_edittext_dark"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/story_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Story Description"
android:textColor="@android:color/widget_edittext_dark"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Mainacctivity. java
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<Story_Details> story_details;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.reycler_view);
story_details = new ArrayList<>();
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
recyclerView.setLayoutManager(new GridLayoutManager(this,1));
RecylerViewAdapter recylerViewAdapter = new RecylerViewAdapter(MainActivity.this,story_details);
recyclerView.setAdapter(recylerViewAdapter);
}
}
AnimalActitivy. java
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<Story_Details> story_details;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.reycler_view);
story_details = new ArrayList<>();
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
story_details.add(new Story_Details("Animal","All About Animal stories ",R.drawable.animals));
recyclerView.setLayoutManager(new GridLayoutManager(this,1));
RecylerViewAdapter recylerViewAdapter = new RecylerViewAdapter(MainActivity.this,story_details);
recyclerView.setAdapter(recylerViewAdapter);
}
}
Animal_Item_Adapter
public class Animal_item_adapter extends RecyclerView.Adapter<Animal_item_adapter.ViewHolder> {
private Context context;
private List<Animal_details> animal_details;
public Animal_item_adapter(Context mContext, List<Animal_details> animal_details) {
this.context = mContext;
this.animal_details = animal_details;
}
@NonNull
@Override
public Animal_item_adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(context).inflate(R.layout.book_activity,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.animal_logo.setImageResource(animal_details.get(position).getThumb());
holder.animal_title.setText(animal_details.get(position).getTitle());
holder.animal_description.setText(animal_details.get(position).getDescription());
}
@Override
public int getItemCount() {
return animal_details.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView animal_logo;
private TextView animal_title , animal_description;
private LinearLayout animallayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
animal_logo = itemView.findViewById(R.id.animal_logo);
animal_title = itemView.findViewById(R.id.animal_title);
animal_description = itemView.findViewById(R.id.animal_description);
animallayout = itemView.findViewById(R.id.animallayout);
}
}
}
Recyler_View_Adapter
public class RecylerViewAdapter extends RecyclerView.Adapter<RecylerViewAdapter.ViewHolder> {
private Context mContext;
private List<Story_Details> story_details;
LinearLayout linearLayout;
public RecylerViewAdapter(Context mContext, List<Story_Details> story_details) {
this.mContext = mContext;
this.story_details = story_details;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(mContext).inflate(R.layout.book_activity,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.book_logo_img.setImageResource(story_details.get(position).getThumb());
holder.story_title.setText(story_details.get(position).getTitle());
holder.story_description.setText(story_details.get(position).getDescription());
}
@Override
public int getItemCount() {
return story_details.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView book_logo_img;
private TextView story_title , story_description;
private LinearLayout linear_layout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
book_logo_img = itemView.findViewById(R.id.book_logo_img);
story_title = itemView.findViewById(R.id.story_title);
story_description = itemView.findViewById(R.id.story_description);
linear_layout = itemView.findViewById(R.id.linear_layout);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent;
switch (getAdapterPosition()){
case 0:
intent = new Intent(v.getContext(), AnimalActivity.class);
v.getContext().startActivity(intent);
break;
}
}
});
}
}
}
Animal_details. java
public class Animal_details {
private String Title;
private String Description;
private int Thumb;
public Animal_details() {
}
public Animal_details(String title, String description, int thumb) {
Title = title;
Description = description;
Thumb = thumb;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public int getThumb() {
return Thumb;
}
public void setThumb(int thumb) {
Thumb = thumb;
}
}
Story_details. java
public class Story_Details {
private String Title;
private String Description;
private int Thumb;
public Story_Details() {
}
public Story_Details(String title, String description, int thumb) {
Title = title;
Description = description;
Thumb = thumb;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public int getThumb() {
return Thumb;
}
public void setThumb(int thumb) {
Thumb = thumb;
}
}