Java 我在soundAdapter类中遇到一个错误,我不知道如何解决它
我犯了这个错误,我不知道如何解决它。我正在制作一个包含列表的应用程序,比如tiktok。我将它的设计分为两种布局。我是一个初学者,我真的不知道去哪里看,如何解决它,我希望那些谁知道它更好地解释给我的错误和解决办法Java 我在soundAdapter类中遇到一个错误,我不知道如何解决它,java,android,Java,Android,我犯了这个错误,我不知道如何解决它。我正在制作一个包含列表的应用程序,比如tiktok。我将它的设计分为两种布局。我是一个初学者,我真的不知道去哪里看,如何解决它,我希望那些谁知道它更好地解释给我的错误和解决办法 java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at com.anisa
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);
}
}
}
导入android.content.Context;
导入android.content.Intent;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.TextView;
导入androidx.annotation.NonNull;
导入androidx.recyclerview.widget.recyclerview;
导入com.anisacode.expriment.Model.SoundModel;
进口com.anisacoding.EXPLIMENT.R;
导入com.anisacode.expriment.VideoEditorFolder.cameraActivity;
导入java.util.List;
公共类SoundAdapter扩展了RecyclerView.Adapter{
列表声音模型列表;
语境;
公共SoundAdapter(ListsoundModelList,上下文){
this.soundModelList=soundModelList;
this.context=上下文;
}
@非空
@凌驾
public SoundAdapter.SoundViewHolder onCreateViewHolder(@NonNull ViewGroup ViewGroup,int viewType){
View View=LayoutInflater.from(viewGroup.getContext()).flate(R.layout.layout\u声音,viewGroup,false);
返回新的SoundViewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(@NonNull SoundAdapter.SoundViewHolder holder,int位置){
SoundModel SoundModel=soundModelList.get(位置);
holder.sound_title.setText(soundModel.getSound_title());
holder.itemView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向意向=新意向(上下文、活动类);
intent.putExtra(“sound_url”,soundModel.getSound_file());
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
背景。开始触觉(意图);
}
});
}
@凌驾
public int getItemCount(){
返回soundModelList.size();
}
公共静态类SoundViewHolder扩展了RecyclerView.ViewHolder{
私有文本视图声音标题;
公共SoundViewHolder(@NonNull View 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>
我认为您列出了空对象引用。在将数据从活动或片段发送到适配器构造函数之前,请确保列表已初始化
公共SoundAdapter(列表(空格)soundModelList,上下文上下文)空格也您的
soundModelList
为null
。如何实例化适配器?你传递的值是多少?