对空引用引用的notifyDataSetChanged()

时间:2016-01-31 00:20:07

标签: android

我使用startActivityForResult启动另一个活动来输入一些信息,然后将意图传回去。以下是部分代码扩展ListActivity:

private ArrayList<Book> shoppingCart = new ArrayList<Book>();
ViewHolderAdapter viewHolderAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO check if there is saved UI state, and if so, restore it (i.e. the cart contents)

    if (shoppingCart.isEmpty())
        getListView().setEmptyView(findViewById(android.R.id.empty));
    else
    {
        viewHolderAdapter = new ViewHolderAdapter(this, R.layout.cart_row, shoppingCart);
        setListAdapter(viewHolderAdapter);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.bookstore_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch (item.getItemId())
    {
        case R.id.add:
            Intent intent = new Intent(this, AddBookActivity.class);
            startActivityForResult(intent, ADD_REQUEST);
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if(requestCode == ADD_REQUEST) {
        if (resultCode == RESULT_OK) {
        //  Log.d(TAG, "*************************************");
            Bundle data = intent.getExtras();
            Book book = (Book) data.getParcelable("book");
            shoppingCart.add(book);

            viewHolderAdapter.notifyDataSetChanged();
        }
    }

}

然而,他们是一个错误:

Attempt to invoke virtual method 'void edu.stevens.cs522.bookstore.activities.BookStoreActivity$ViewHolderAdapter.notifyDataSetChanged()' on a null object reference

这是发回意图的按钮:

Button add_btn = (Button) findViewById(R.id.add_one);
    add_btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent intent = getIntent();
            EditText titleView = (EditText) findViewById(R.id.search_title);
            EditText authorView = (EditText) findViewById(R.id.search_author);
            EditText isbnView = (EditText) findViewById(R.id.search_isbn);
            EditText priceView = (EditText) findViewById(R.id.search_price);

            String author_str = authorView.getText().toString();
            String[] split_str = author_str.split("\\s+");
            Author author = null;
            if (split_str.length == 2) {
                author = new Author(split_str[0], split_str[1]);
            }
            if (split_str.length == 3) {
                author = new Author(split_str[0], split_str[1], split_str[2]);
            }

            String title = titleView.getText().toString();
            String isbn = isbnView.getText().toString();
            String price = priceView.getText().toString();

            intent.putExtra("book", new Book(title, author, isbn, price));
            setResult(Activity.RESULT_OK,intent);
            finish();
        }
    });

提前致谢。

1 个答案:

答案 0 :(得分:1)

您需要确保始终初始化viewHolderAdapter。将您的onCreate方法更新为以下内容。请注意,始终为viewHolderAdapter分配值。也可以随时调用setEmptyView,因为仅在您的适配器为空时才显示它。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO check if there is saved UI state, and if so, restore it (i.e. the cart contents)
    getListView().setEmptyView(findViewById(android.R.id.empty));
    viewHolderAdapter = new ViewHolderAdapter(this, R.layout.cart_row, shoppingCart)
    setListAdapter(viewHolderAdapter);
}