我正在尝试从其他类访问自定义数组列表的内容,以在布局上显示文本和图像。没有构建错误,但是当我尝试从列表中打开布局时,我的应用程序崩溃。我只能从Android Studio中的日志猫中访问错误
我的活动
public class PlayingActivity extends AppCompatActivity {
private Song songs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playing);
// Find the image view in the activity_playing.xml layout with the ID playing_album_art_img_view.
ImageView imageView = findViewById(R.id.playing_album_art_img_view);
// Get the Album art from the currentSong object and set this text on
// the song ImageView
imageView.setImageResource(songs.getAlbumArt());
// Find the TextView in the activity_playing.xml layout with the ID playing_artiste_text_view.
TextView pArtisteTextView = findViewById(R.id.playing_artiste_text_view);
// Get the Artiste from the currentSong object and set this text on
// the Artiste TextView.
pArtisteTextView.setText(songs.getArtiste());
// Find the TextView in the activity_playing.xml layout with the ID playing_song_text_view.
TextView pSongTitleTextView = findViewById(R.id.playing_song_text_view);
// Get the song title from the currentSong object and set this text on
// the song title TextView.
pSongTitleTextView.setText(songs.getSongTitle());
}
` 我的歌声课
public class Song extends ArrayList<Song> {
/** Song title */
private String mSongTitle;
/** Artiste's name */
private String mArtiste;
// **歌曲的专辑封面* / 私人Integer mAlbumArt;
/**
* Create a new Song object.
*
* @param songTitle is the song item user is about to play
*
* @param Artiste is the musician that owns the song
* *
* @param albumArt is the album art image of the song
*/
public Song(String songTitle, String Artiste, int albumArt)
/*public Song(String songTitle, String Artiste)*/{
mSongTitle = songTitle;
mArtiste = Artiste;
mAlbumArt = albumArt;
}
/**
* Get the name of song.
*/
public String getSongTitle() {
return mSongTitle;
}
/**
* Get the artiste
*/
public String getArtiste() {
return mArtiste;
}
/**
* Get the album art
*/
public Integer getAlbumArt() { return mAlbumArt; }
}
和SongAdapter用于我的listView
public SongAdapter(Context context, ArrayList<Song> songs) {
super(context, 0, songs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.song_item, parent, false);
}
// Get the {@link Song} object located at this position in the list
Song currentSong = getItem(position);
// Find the TextView in the song_item.xml layout with the ID artiste_text_view.
TextView artisteTextView = listItemView.findViewById(R.id.artiste_text_view);
// Get the Artiste from the currentSong object and set this text on
// the Artiste TextView.
artisteTextView.setText(currentSong.getArtiste());
// Find the TextView in the song_item.xml layout with the ID song_title_text_view.
TextView songTitleTextView = listItemView.findViewById(R.id.song_title_text_view);
// Get the song title from the currentSong object and set this text on
// the song title TextView.
songTitleTextView.setText(currentSong.getSongTitle());
// Find the ImageView in the song_item.xml layout with the ID playing_album_art_img_view.
ImageView songAlbumArtImageView = listItemView.findViewById(R.id.playing_album_art_img_view);
// Get the song album art from the currentSong object and set this text on
// the song album art ImageView.
songAlbumArtImageView.setImageResource(currentSong.getAlbumArt());
// Return the whole list item layout (containing 2 TextViews) so that it can be shown in
// the ListView.
return listItemView;
}
我的song_item布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/song_title_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Ada" />
<TextView
android:id="@+id/artiste_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Flavour" />
这是我的错误消息
Process: com.example.android.musicalstructureapp, PID: 30680
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
at com.example.android.musicalstructureapp.SongAdapter.getView(SongAdapter.java:58)
at android.widget.AbsListView.obtainView(AbsListView.java:2491)
答案 0 :(得分:3)
您的这一行有问题:
ImageView songAlbumArtImageView = listItemView.findViewById(R.id.playing_album_art_img_view); // May be id is wrong.
songAlbumArtImageView.setImageResource(currentSong.getAlbumArt()); // So it's crashing here.