Null Pointer使用getListView()更新自定义列表视图中的单行时出现异常

Sre*_*ree 0 android android-listview

我想更新自定义列表视图中的单行.我不是在编写完整的代码,因为它太长了.只写足够的部分.

我的listview显示正确我也知道如何更新单行.

由于空指针异常我无法继续我的代码..请帮助!

ProductList.java

public class ProductList extends ListActivity {

ListView listProduct;

protected void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.activity_product_list);
listProduct = getListView();
ProductAdapter productAdapter;
productAdapter = new ProductAdapter(ProductList.this,
                R.layout.productlistview, addedList);
listProduct.setAdapter(productAdapter); 

}

public void updateList(Context context){
        int a = listProduct.getFirstVisiblePosition(); // Here I get Null Pointer Exeption
        Toast.makeText(context,
                a + " is listadapter " ,
                Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

ProductAdapter.java

public class ProductAdapter extends ArrayAdapter<SetGetProduct> {

// initialization codes
//getView() method - This properly displays my custom list view
// On the list view I have an ADD - Button
// On pressing that button a dialog box appears to enter new quantity.
// I want to update the new quantity to the purticular row

final Dialog quantityDialog = new Dialog(context);
quantityDialog.setContentView(R.layout.quantity_customdialogue);
quantityDialog.setTitle("Enter Quantity :");
final EditText et_cus_quantity = (EditText) quantityDialog
                        .findViewById(R.id.et_cusdialog_quantity);
                Button btn_quantityOK = (Button) quantityDialog
                        .findViewById(R.id.btn_custom_quantityOK);
btn_quantityOK.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

quantityDialog.dismiss();
new ProductList().updateList(context);

}
}

}
Run Code Online (Sandbox Code Playgroud)

Lal*_*ani 5

您无法创建Activity/ListActivity使用new operator和访问它的实例.

new ProductList().updateList(context); //这是访问Activity实例的错误方法

相反,您可以在getView()方法内更新Adapter类本身的ListView行.

要么

您可以Activity在其构造函数中传递Adapter类中的实例来访问它.