Я получаю сообщение об ошибке в этой строке:
intent.putParcelableArrayListExtra("songList", songsByAlbum);
Что говорит:
Attempt to invoke virtual method 'void android.view.ViewGroup.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
, когда я помещаю эту строку в комментарии, она переходит к моей другой активности ListSongsActivity, поэтомуошибка уходит.
Так что я уверен, что эта строка вызывает ошибку.
Я понятия не имею, почему эта ошибка возникает, прежде чем я перешел на Android X, она работала нормально, но было бы странно, если бы этопричина?
Есть идеи, как мне это решить?
Код для получения песен по конкретному альбому.
songsByAlbum является Arraylist с типом 'Song', который реализует Parcelable .
Я также проверил, и songByAlbum не является нулевым и содержит значения, когда я передаю нужное название альбома.
@Override
public void onClickAlbum(int position) {
if (getActivity() != null) {
String selectedAlbum = albumsList.get(position).getAlbum();
songsByAlbum = Main.songs.getSongsByAlbum(selectedAlbum);
Intent intent = new Intent(getActivity(), ListSongsActivity.class);
intent.putExtra("title", selectedAlbum);
intent.putParcelableArrayListExtra("songList", songsByAlbum);
startActivity(intent);
Log.d(TAG,"albumList: " + albumsList.get(position).getAlbum());
Log.d(TAG, "songsByAlbum: " + songsByAlbum);
Toast.makeText(getActivity(), "this works!", Toast.LENGTH_SHORT).show();
}
}
Main.songs.getSongsByAlbum Метод
public ArrayList<Song> getSongsByAlbum(String desiredAlbum) {
ArrayList<Song> songsByAlbum = new ArrayList<>();
for (Song song : songs) {
String currentAlbum = song.getAlbum();
if (Objects.equals(currentAlbum, desiredAlbum)) {
songsByAlbum.add(song);
}
}
return songsByAlbum;
}
LOGCAT
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vince_mp3player.mp3icious, PID: 32642
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at c.c.a.c.k.a(:115)
at c.c.a.c.k.b(:22)
at androidx.recyclerview.widget.RecyclerView$g.c(:6781)
at androidx.recyclerview.widget.RecyclerView$g.a(:6823)
at androidx.recyclerview.widget.RecyclerView$v.a(:5752)
at androidx.recyclerview.widget.RecyclerView$v.a(:6019)
at androidx.recyclerview.widget.RecyclerView$v.b(:5858)
at androidx.recyclerview.widget.RecyclerView$v.d(:5854)
at androidx.recyclerview.widget.LinearLayoutManager$c.a(:2230)
at androidx.recyclerview.widget.LinearLayoutManager.a(:1557)
at androidx.recyclerview.widget.LinearLayoutManager.a(:1517)
at androidx.recyclerview.widget.LinearLayoutManager.c(:612)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(:3924)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(:3641)
at androidx.recyclerview.widget.RecyclerView.onLayout(:4194)
at android.view.View.layout(View.java:20676)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20676)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20676)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
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:20676)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20676)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
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:20676)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
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:753)
at android.view.View.layout(View.java:20676)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2799)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2316)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1463)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7202)
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:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Класс Parcelable
import android.os.Parcel;
import android.os.Parcelable;
public class Song implements Parcelable {
private long id;
private String data;
private String title;
private String artist;
private long albumid;
private String album;
private long genreid;
private String genre;
private long duration;
private int year;
private int dateAdded;
private String file_name;
private String file_path;
private String file_parent_path;
public Song(){
}
protected Song(Parcel in) {
id = in.readLong();
data = in.readString();
title = in.readString();
artist = in.readString();
albumid = in.readLong();
album = in.readString();
genreid = in.readLong();
genre = in.readString();
duration = in.readLong();
year = in.readInt();
dateAdded = in.readInt();
}
public static final Creator<Song> CREATOR = new Creator<Song>() {
@Override
public Song createFromParcel(Parcel in) {
return new Song(in);
}
@Override
public Song[] newArray(int size) {
return new Song[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeString(data);
dest.writeString(title);
dest.writeString(artist);
dest.writeLong(albumid);
dest.writeString(album);
dest.writeLong(genreid);
dest.writeString(genre);
dest.writeLong(duration);
dest.writeInt(year);
dest.writeInt(dateAdded);
}