【发布时间】:2020-01-27 03:00:36
【问题描述】:
我正在尝试调用另一个类中的方法,当在同样位于 AlertDialogInput 中的 AutoCompleteTextView 中单击项目时,该方法将填充 TextViews。
但是当我点击一个项目时我的应用程序崩溃了。
我是 android 开发的新手,因此非常感谢任何帮助。
自定义警报对话框 警报对话框类
public static class ToPrintAccountSearchDialog extends AppCompatDialogFragment
{
private AutoCompleteTextView toprint_auto_account_search_dialog;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.for_printing_account_search_dialog_layout, null);
builder.setView(view);
builder.setTitle("Search Account");
List<ForBillPrintConsumerEntities> forBillPrintConsumerEntities = new ArrayList<>();
toprint_auto_account_search_dialog = view.findViewById(R.id.Alert_Dialog_Account_Auto_Search);
ForPrintingConsumerAccountSearchAdapter forPrintingConsumerAccountSearchAdapter = new ForPrintingConsumerAccountSearchAdapter(getActivity(), forBillPrintConsumerEntities);
toprint_auto_account_search_dialog.setThreshold(1);
toprint_auto_account_search_dialog.setAdapter(forPrintingConsumerAccountSearchAdapter);
toprint_auto_account_search_dialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
PrintBill printBill = new PrintBill();
printBill.getotherinformationbyaccountforprinting();
}
});
return builder.create();
}
}
这是显示 AlertDialogInput 的代码 主要活动
Account_No = findViewById(R.id.toprint_Account_No_Value);
Account_No.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
forPrintAccountSearchDialog();
}
});
public void forPrintAccountSearchDialog()
{
ToPrintAccountSearchDialog toPrintAccountSearchDialog = new ToPrintAccountSearchDialog();
toPrintAccountSearchDialog.show(getSupportFragmentManager(), "ToPrintAccountSearchDialog");
}
这是我想要在 AlertDialog AutocompleteTextview 中单击项目时调用的方法,该项目也在主活动中,以填充我的主活动中的文本视图
public void getotherinformationbyaccountforprinting()
{
ConsumerAccountForPrinting = toprint_auto_account_search_dialog.getText().toString();
db = new DatabaseHelper(getApplicationContext());
sqLiteDatabase = db.getReadableDatabase();
Cursor cursor = db.ForPrintingGetOtherInfoByAccount(ConsumerAccountForPrinting, sqLiteDatabase);
if (cursor.moveToFirst()) {
do {
String ACCOUNT_NUMBER = cursor.getString(0);
String NAME = cursor.getString(1);
String ADDRESS = cursor.getString(2);
Account_No.setText(ACCOUNT_NUMBER);
Name.setText(NAME);
Address.setText(ADDRESS);
}
while (cursor.moveToNext());
}
}
数据库助手
public Cursor ForPrintingGetOtherInfoByAccount(String keyword, SQLiteDatabase sqLiteDatabase)
{
String [] projections = {ForPrintingConsumerOtherInfoAdapterByAccount.getOtherInfoForPrintingByAccount.ACCOUNT_NO,
ForPrintingConsumerOtherInfoAdapterByAccount.getOtherInfoForPrintingByAccount.NAME,
ForPrintingConsumerOtherInfoAdapterByAccount.getOtherInfoForPrintingByAccount.ADDRESS,
String selection = ConsumerListOtherInfoAdapterByAccount.getOtherInfoForListByAccount.CONSUMER_ACCOUNT_NO+" LIKE ?";
String [] selection_args = {keyword};
Cursor cursor = sqLiteDatabase.query(ForPrintingConsumerOtherInfoAdapterByAccount.getOtherInfoForPrintingByAccount.TABLE_NAME,projections,selection,selection_args,null,null,null);
return cursor;
}
在数据库助手内部调用的投影
public class ForPrintingConsumerOtherInfoAdapterByAccount
{
public static abstract class getOtherInfoForPrintingByAccount
{
public static final String TABLE_NAME = "toPrintBill";
public static final String ACCOUNT_NO = "account_no";
public static final String NAME = "name";
public static final String ADDRESS = "address";
}
}
点击时弹出文本视图提示对话框
当我输入数字时,autocompletetextview 将建议从 sqlite 数据库表中具有相同值的数据
但是当我点击一个项目时会发生这种情况
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.AutoCompleteTextView.getText()' on a null object reference
at com.vicjames.qiimeterreader.PrintBill.getotherinformationbyaccountforprinting(PrintBill.java:224)
at com.vicjames.qiimeterreader.PrintBill$ToPrintAccountSearchDialog$1.onItemClick(PrintBill.java:189)
at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:1017)
如何在我的警报对话框类和主要活动之间创建通信,以便我可以使用该方法?
【问题讨论】:
-
请查看this answer。在这个答案中,他们使用了 Fragment。您可以在
Dialogs中使用interface实现类似的含义
标签: android android-alertdialog