[我正在尝试调用另一个类内的方法,当在AutoCompleteTextView内(也位于AlertDialogInput内的情况下)单击某个项目时,该类将填充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中单击项目时要调用的方法,该方法也在Main Activity中以填充我的textview insid主要活动中
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)
我如何在我的警报对话框类和主要活动之间建立通信,以便可以使用该方法?
您打电话给findViewById
太早了:
Account_No = findViewById(R.id.toprint_Account_No_Value);
这在构造活动时调用它。尝试用Account_No
替换getotherinformationbyaccountforprinting
中的findViewById(R.id.toprint_Account_No_Value)
,或在调用Account_No
之后在onCreate
中设置setContentView
。
正如我在评论中建议的那样,您可以使用interface
在Activity / Fragments / Adapters / etc`之间进行通信。
public interface DataTransfer{
void sendData(String data); //change parameter to whatever you want
}
ToPrintAccountSearchDialog toPrintAccountSearchDialog =
new ToPrintAccountSearchDialog(this);
// here this will pass the implemented interface
private DataTransfer dataTransfer;
public ToPrintAccountSearchDialog(DataTransfer dataTransfer){
this.dataTransfer = dataTransfer;
}
dataTransfer.sendData("Some important data");
@Override
方法void sendData(String data);
@Override
public void sendData(String data) {
Log.e("TAG", "sendData: this data is from Dialog " + data);
}
有1个活动A
和1个DialogFragment B
[B
想要发送数据到A
[A
给B
一个interface
,所以当B
调用dataTransfer.sendData("Hello");
A
也将称为自己的overridden
sendData(String data);
现在如果您打印此页
@Override
public void sendData(String data) {
Log.e("TAG", "sendData: " + data);
}
将打印
Hello
您可以从method
呼叫其他任何sendData
希望这会有所帮助!
请询问是否需要更多帮助