在BaseAdapter中使用getView的NullPointerException

时间:2017-11-23 20:33:32

标签: java android listview nullpointerexception

我正在尝试填充listView并且正在获取nullPointerException。当我调试程序时,我看到TextView都是null,即。由于某种原因,findViewById无法正常工作。有什么建议吗?

favouritesActivity填充activity_favourites listView,使用favourite_list_layout作为列表中每个元素的格式。

填充列表的活动

public class favouritesActivity extends AppCompatActivity {
    String[] titles, authors, publishers, ISBN;
    DBConnection dbConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favourites);

        ListView favouritesListView = (ListView) findViewById(R.id.favouritesListView);

        //Below populates title and author string lists
        //connecting to DB
        dbConnection = new DBConnection(this);

        //populating list with
        List<Book> favouriteBookList = dbConnection.getAllFavBooks();

        //below i am populating lists with book data (title and author)
        titles = new String[favouriteBookList.size()];

        for (int i=0; i<favouriteBookList.size(); i++){
            titles[i] = favouriteBookList.get(i).title;
        }

        authors = new String[favouriteBookList.size()];

        for (int i=0; i<favouriteBookList.size(); i++){
            authors[i] = favouriteBookList.get(i).authorsString;
        }

        publishers = new String[favouriteBookList.size()];

        for (int i=0; i<favouriteBookList.size(); i++){
            publishers[i] = favouriteBookList.get(i).publisher;
        }

        ISBN = new String[favouriteBookList.size()];

        for (int i=0; i<favouriteBookList.size(); i++){
            ISBN[i] = favouriteBookList.get(i).isbn;
        }

        CustomAdapter customAdapter = new CustomAdapter();
        favouritesListView.setAdapter(customAdapter);

    }

    class CustomAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = getLayoutInflater().inflate(R.layout.favourite_list_layout, null);

            final int index = i;

            TextView titleTV = (TextView) findViewById(R.id.favouriteTitle);
            TextView authorTV = (TextView) findViewById(R.id.favouriteAuthor);
            TextView publisherTV = (TextView) findViewById(R.id.favouritePublisher);
            Button deleteFavButton = (Button) findViewById(R.id.deleteFavButton);

            titleTV.setText(titles[index]);
            authorTV.setText(authors[index]);
            publisherTV.setText(publishers[index]);
            deleteFavButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dbConnection.removeFavBook(ISBN[index]);
                }
            });
            return view;
        }
    }
}

favourite_list_layout - 列表中每个元素的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:id="@+id/favouriteTitle"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="32dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/favouriteAuthor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/favouriteTitle"
        android:layout_alignStart="@+id/favouriteTitle"
        android:layout_below="@+id/favouriteTitle"
        android:text="TextView" />

    <TextView
        android:id="@+id/favouritePublisher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/favouriteAuthor"
        android:layout_alignStart="@+id/favouriteAuthor"
        android:layout_below="@+id/favouriteAuthor"
        android:text="TextView" />

    <Button
        android:id="@+id/deleteFavButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/favouriteAuthor"
        android:layout_alignBottom="@+id/favouriteAuthor"
        android:layout_marginLeft="38dp"
        android:layout_marginStart="38dp"
        android:layout_toEndOf="@+id/favouriteTitle"
        android:layout_toRightOf="@+id/favouriteTitle"
        android:text="Delete" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:-1)

您需要在充气的布局上调用df.to_delayed,而不是在您的活动上。也就是说,替换

findViewById()

TextView titleTV = (TextView) findViewById(R.id.favouriteTitle);

和您的其他观点类似。