尝试使用listView.setAdapter方法设置自定义arrayAdapter并获取nullpointer异常,尽管有明显的路径

时间:2018-01-03 01:32:45

标签: android listview

我知道当代码没有任何东西可以抓取方法时会发生这些异常,但我不确定为什么我的预期路径不起作用。我有一个自定义适配器(MusicAdapter)抓取项目列表,将其设置为listView.xml文件,并使用setAdapter()设置屏幕。

尝试此操作时,我收到错误消息 尝试调用虚方法' void android.widget.ListView.setAdapter(android.widget.ListAdapter)'在null对象引用上。

MyMusicActivity.java文件:

package com.example.android.musicapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import java.util.ArrayList;

public class MyMusicActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    ArrayList<Music> music = new ArrayList<Music>();

    //nonsense examples
    music.add(new Music("The Who", "Won't Get Fooled Again", "Who's Next"));
    music.add(new Music("mother?", "әṭa","as"));
    music.add(new Music("mother?", "әṭa","as"));
    music.add(new Music("mother?", "әṭa","as"));
    music.add(new Music("mother?", "әṭa","as"));
    music.add(new Music("mother?", "әṭa","as"));
    music.add(new Music("mother?", "әṭa","as"));
    music.add(new Music("mother?", "әṭa","as"));
    music.add(new Music("mother?", "әṭa","as"));

    MusicAdapter adapter = new MusicAdapter(this, music);
    ListView listView = (ListView) findViewById(R.id.music_list);
    listView.setAdapter(adapter);
    }
}

music_item.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/music_list"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

MusicAdapter文件:

package com.example.android.musicapp;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;

public class MusicAdapter extends ArrayAdapter<Music> {
public MusicAdapter(Activity context, ArrayList<Music> words) {
    // Here, we initialize the ArrayAdapter's internal storage for the context and the list.
    // the second argument is used when the ArrayAdapter is populating a single TextView.
    // Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
    // going to use this second argument, so it can be any value. Here, we used 0.
    super(context, 0, words);
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if the existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if(listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.music_item, parent, false);
    }

    // Get the {@link Music} object located at this position in the list
    Music currentMusic = getItem(position);

    // Find the TextView in the music_item.xml layout with the artist text
    TextView artistTextView = (TextView) listItemView.findViewById(R.id.artist_text_view);
    // Get the artist from the current music object and
    // set this text on the number TextView
    artistTextView.setText(currentMusic.getArtist());

    // Find the TextView in the music_item.xml layout with the title text
    TextView titleTextView = (TextView) listItemView.findViewById(R.id.title_text_view);
    // Get the title from the current music object and
    // set this text on the name TextView
    titleTextView.setText(currentMusic.getTitle());

    // Find the TextView in the music_item.xml layout with the album text
    TextView albumTextView = (TextView) listItemView.findViewById(R.id.album_text_view);
    // Get the album from the current music object and
    // set this text on the name TextView
    albumTextView.setText(currentMusic.getAlbum());

    // Return the whole list item layout (containing 3 TextViews)
    // so that it can be shown in the ListView
    return listItemView;
    }
}

1 个答案:

答案 0 :(得分:2)

您似乎没有为活动设置contentView。错误是说找不到listView。在onCreate的顶部,请务必致电setContentView(R.layout.[YOUR_LAYOUT_XML])

相关问题