嘿,我正在尝试使用EditText
中的值填充新对话框中的ListView
。问题是我得到了
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object
我对从ListView
获得的值进行Toast并且它有效,它返回特定值。
当我尝试拨打.setText(ValueFromListView)
时,它会出错并且我不明白为什么。谁能帮我这个?
这是代码:
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setTitle("Edit item...");
dialog.setContentView(R.layout.custom_dialog_edit_delete);
dialog.show();
Button button_UPDATE = (Button) dialog.findViewById(R.id.custom_dialog_edit_delete_button_UPDATE);
Button button_CANCEL = (Button) dialog.findViewById(R.id.custom_dialog_edit_delete_button_CANCEL);
Button button_DELETE = (Button) dialog.findViewById(R.id.custom_dialog_edit_delete_button_DELETE);
// GET PRODUCT REFERENCES
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String productName = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_PRODUCT));
String productPrice = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_PRICE));
String productType = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_TYPE));
Toast.makeText(MainActivity.this, productName + " " + productPrice + " " + productType, Toast.LENGTH_SHORT).show();
EditText prodField = (EditText) findViewById(R.id.custom_dialog_edit_delete__productName);
prodField.setText(productName); // This is the line that i get the null pointer exception
}
答案 0 :(得分:3)
EditText
,因此在创建EditText
对象时必须提供yout对话框的参考。
EditText prodField = (EditText) dialog.findViewById(R.id.custom_dialog_edit_delete__productName);
答案 1 :(得分:0)
试试这个
EditText prodField = (EditText)dialog.findViewById(R.id.custom_dialog_edit_delete__productName);
prodField.setText(productName);
//