java.lang.NullPointerException-应用程序不断崩溃
我试图从另一个类访问自定义arraylist的内容,以便在版面上显示文本和图像。没有生成错误,但是当我尝试从列表中打开布局时,我的应用程序崩溃。我只能通过Android studio中的日志猫访问错误 我的活动java.lang.NullPointerException-应用程序不断崩溃,java,android,nullpointerexception,Java,Android,Nullpointerexception,我试图从另一个类访问自定义arraylist的内容,以便在版面上显示文本和图像。没有生成错误,但是当我尝试从列表中打开布局时,我的应用程序崩溃。我只能通过Android studio中的日志猫访问错误 我的活动 public class PlayingActivity extends AppCompatActivity { private Song songs; @Override protected void onCreate(Bundle savedInstanc
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;
和我的listView的SongAdapter
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;
}
您的这条线路有故障:
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.
对于代码中的所有注释,我会给你+1:DAdd你的
song_项目
code..song_项目使用Arrayadapter填充列表视图谢谢你的回复。。即使在我删除了与ImageView相关的所有内容之后,我也会收到与TextView相同的错误,如下所示:您可以发布您的R.layout.song\u项目
xml文件吗?有两种可能是您正在获取的ID错误,否则您的currentSong
对象为空。我已添加了我的song\u项目布局文件,但我认为这与我的currentSong有关。通过删除我的问题的ImageView实现,我避开了NPE,但是对于其他遇到类似问题的人。。更好的方法是在我的word类中添加一个新的构造函数,该构造函数将处理带有图像的视图,以避免没有图像的视图导致错误
<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)
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.