首页 > 解决方案 > 我收到一个错误,在 soundAdapter 类中,我不知道如何解决它

问题描述

我得到了这个错误,我不知道如何解决它。我制作了一个带有列表的应用程序,例如 tiktok。我将它的设计分为两种布局。我是一个初学者,我真的不知道在哪里寻找以及如何解决它,我希望那些更了解它的人会向我解释错误和解决方案。

 java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at com.anisacoding.expriment.Adapter.SoundAdapter.getItemCount(SoundAdapter.java:55)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4044)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3849)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:22419)
        at android.view.ViewGroup.layout(ViewGroup.java:6584)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1762)
        at android.view.View.layout(View.java:22419)
        at android.view.ViewGroup.layout(ViewGroup.java:6584)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22419)
        at android.view.ViewGroup.layout(ViewGroup.java:6584)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:22419)
        at android.view.ViewGroup.layout(ViewGroup.java:6584)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22419)
        at android.view.ViewGroup.layout(ViewGroup.java:6584)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:22419)
        at android.view.ViewGroup.layout(ViewGroup.java:6584)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:1041)
        at android.view.View.layout(View.java:22419)
        at android.view.ViewGroup.layout(ViewGroup.java:6584)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3378)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2842)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1888)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8511)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:696)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7050)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

这是得到错误的类。

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.anisacoding.expriment.Model.SoundModel;
import com.anisacoding.expriment.R;
import com.anisacoding.expriment.VideoEditorFolder.PortraitCameraActivity;

import java.util.List;

public class SoundAdapter extends RecyclerView.Adapter<SoundAdapter.SoundViewHolder> {
List<SoundModel> soundModelList;
Context context;

public SoundAdapter(List<SoundModel>soundModelList, Context context){
    this.soundModelList = soundModelList;
    this.context = context;
}
    @NonNull
    @Override
    public SoundAdapter.SoundViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
       View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_sound, viewGroup, false);
       return new SoundViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SoundAdapter.SoundViewHolder holder, int position) {

    SoundModel soundModel = soundModelList.get(position);

          holder.sound_title.setText(soundModel.getSound_title());
          holder.itemView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent intent = new Intent(context, PortraitCameraActivity.class);
                  intent.putExtra("sound_url", soundModel.getSound_file());
                  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                  context.startActivity(intent);
              }
          });

    }

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

    public static class SoundViewHolder extends RecyclerView.ViewHolder {

    private TextView sound_title;
        public SoundViewHolder(@NonNull View itemView) {
            super(itemView);

            sound_title = itemView.findViewById(R.id.textView);
        }
    }



    }

这是xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">


    <ImageView
        android:id="@+id/imageView20"
        android:layout_width="150dp"
        android:layout_height="147dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="80dp"
        android:layout_marginEnd="8dp"
        android:contentDescription="@string/library_music"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_library_music" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:maxLength="50"
        android:text="Alaf file song ......."
        android:textColor="@android:color/white"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/imageView20"
        app:layout_constraintStart_toStartOf="@+id/imageView20"
        app:layout_constraintTop_toBottomOf="@+id/imageView20"
        app:layout_constraintVertical_bias="0.0" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline24"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="1.0" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

另一个:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:background="@android:color/white"
    tool:context=".Activities.SoundActivity">

    <ImageView
        android:id="@+id/cross"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:layout_marginTop="4dp"
        android:contentDescription="@string/cross"
        android:onClick="backBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_cross" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

标签: javaandroid

解决方案


我认为您列出了给出空对象引用。确保在将数据从活动或片段发送到适配器构造函数之前初始化列表。

公共 SoundAdapter(List(space)soundModelList, Context context) 空间也


推荐阅读