Java 应用程序因“而崩溃”;“空引用上的对象”;我不知道该怎么解决这个问题
我不断地得到错误,但我无法找到解决它的方法,因为一切似乎都是正确的。这很累,因为我看不出我的代码有任何问题。我真的希望有人能帮忙,我很困惑。我想制作一个音乐列表页面,使用recyclerview。我也有一个api 日志:Java 应用程序因“而崩溃”;“空引用上的对象”;我不知道该怎么解决这个问题,java,android-studio,Java,Android Studio,我不断地得到错误,但我无法找到解决它的方法,因为一切似乎都是正确的。这很累,因为我看不出我的代码有任何问题。我真的希望有人能帮忙,我很困惑。我想制作一个音乐列表页面,使用recyclerview。我也有一个api 日志: Process: com.anisacoding.expriment, PID: 24248 java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size(
Process: com.anisacoding.expriment, PID: 24248
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:58)
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)
API接口:
import retrofit2.Call;
import retrofit2.http.GET;
public interface ApiInterface {
/////getting all posts/////
@GET("posts.php")
Call<Users> performAllPosts();
///////getting all sounds//////
@GET("sounds.php")
Call<Users> performAllSounds();
}
用户:
import com.anisacoding.expriment.Model.MediaObject;
import com.anisacoding.expriment.Model.SoundModel;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
public class Users {
@SerializedName("ALL_POSTS")
private ArrayList<MediaObject> AllPosts;
@SerializedName("All_SOUNDS")
private ArrayList<SoundModel> AllSounds;
public ArrayList<MediaObject> getAllPosts(){
return AllPosts;
}
public ArrayList<SoundModel> getAllSounds(){
return AllSounds;
}
}
声音活动:
private RecyclerView recyclerview;
private List<SoundModel> soundModelList = new ArrayList<>();
private SoundAdapter soundAdapter;
public static ApiInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound);
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager3 = new LinearLayoutManager(this);
layoutManager3.setOrientation(RecyclerView.VERTICAL);
recyclerview.setLayoutManager(new GridLayoutManager(this, 2));
Call<Users> call = apiInterface.performAllSounds();
call.enqueue(new Callback<Users>() {
@Override
public void onResponse(Call<Users> call, Response<Users> response) {
if (response.isSuccessful())
{
soundModelList = response.body().getAllSounds();
soundAdapter = new SoundAdapter(soundModelList, getApplicationContext());
recyclerview.setAdapter(soundAdapter);
soundAdapter.notifyDataSetChanged();
} else{
Toast.makeText(SoundActivity.this, "network error", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Users> call, Throwable t) {
Toast.makeText(SoundActivity.this, "network error", Toast.LENGTH_SHORT).show();
}
});
}
public void backBtn (View view){
Intent intent = new Intent(SoundActivity.this, PortraitCameraActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Animatoo.animateInAndOut(this);
finish();
}
@Override
public void onBackPressed () {
Intent intent = new Intent(SoundActivity.this, PortraitCameraActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Animatoo.animateInAndOut(this);
finish();
}
}
私有回收视图回收视图;
私有列表soundModelList=new ArrayList();
专用声卡声卡;
公共静态接口;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动和声音);
apiInterface=ApiClient.getApiClient().create(apiInterface.class);
recyclerview=(recyclerview)findViewById(R.id.recyclerview);
LinearLayoutManager LayoutManager 3=新的LinearLayoutManager(此);
LayoutManager 3.设置方向(RecyclerView.垂直);
setLayoutManager(新的GridLayoutManager(this,2));
Call Call=apinterface.performAllSounds();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful())
{
soundModelList=response.body().getAllSounds();
soundAdapter=新的soundAdapter(soundModelList,getApplicationContext());
recyclerview.setAdapter(声音适配器);
soundAdapter.notifyDataSetChanged();
}否则{
Toast.makeText(SoundActivity.this,“网络错误”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(SoundActivity.this,“网络错误”,Toast.LENGTH_SHORT.show();
}
});
}
公共void backBtn(视图){
意向意向=新意向(SoundActivity.this、cameraActivity.class);
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
星触觉(意向);
Animatoo.animateInAndOut(这个);
完成();
}
@凌驾
反压时的公共无效(){
意向意向=新意向(SoundActivity.this、cameraActivity.class);
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
星触觉(意向);
Animatoo.animateInAndOut(这个);
完成();
}
}
声音适配器:
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 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 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.putExtra("sound_title", soundModel.getSound_title());
context.startActivity(intent);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
}
});
}
@Override
public int getItemCount() {
return soundModelList.size();
}
public class SoundViewHolder extends RecyclerView.ViewHolder {
private TextView sound_title;
public SoundViewHolder(@NonNull View itemView) {
super(itemView);
sound_title = itemView.findViewById(R.id.music_title);
}
}
}
导入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(列表soundModelList,上下文){
this.soundModelList=soundModelList;
this.context=上下文;
}
@非空
@凌驾
public SoundViewHolder onCreateViewHolder(@NonNull ViewGroup ViewGroup,int viewType){
View View=LayoutInflater.from(viewGroup.getContext()).flate(R.layout.layout\u声音,viewGroup,false);
返回新的SoundViewHolder(视图);
}
@凌驾
公共无效onBindViewHolder(@NonNull 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.putExtra(“sound_title”,soundModel.getSound_title());
背景。开始触觉(意图);
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\u title=itemView.findviewbyd(R.id.music\u title);
}
}
}
布局:
<?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="157dp"
android:layout_height="141dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:contentDescription="@string/library_music"
app:tint="#000000"
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_library_music" />
<TextView
android:id="@+id/music_title"
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.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:tools="http://schemas.android.com/tools"
android:background="@android:color/white"
tools: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="408dp"
android:layout_height="730dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.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"
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="157dp"
android:layout_height="141dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:contentDescription="@string/library_music"
app:tint="#000000"
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_library_music" />
<TextView
android:id="@+id/music_title"
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.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:tools="http://schemas.android.com/tools"
android:background="@android:color/white"
tools: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="408dp"
android:layout_height="730dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
错误本身说明:
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:58)
因此,您应该查看您的SoundAdapter.getItemCount
,在那里使用List.size()
时会得到一个NPE。
在这种情况下,请确保初始化列表!=) 为什么
@SerializedName(“ALL_POSTS”)
是大写的,而@SerializedName(“ALL_SOUNDS”)
是小写的l
而不是“ALL”这个词中的l
?您得到的NullPointerException是因为AllSounds
-列表是null
。所以序列化失败了,我认为这与那些小写字母有关。但是我不太熟悉com.google.gson.annotations
的@SerializedName
注释是如何工作的,所以也许我弄错了。我太爱你了❤️ , 成功了。