I'm trying to call a method that is inside another class that will populate TextViews when an item is clicked inside AutoCompleteTextView that is also inside an AlertDialogInput.
But my app crashes when I click an Item.
I'm new in android development so any help would be pretty much appreciated.
Custom Alert Dialog Alert Dialog Class
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();
}
}
This is the code to show AlertDialogInput Main Activity
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");
}
this is the method that I want to call when an Item is clicked inside AlertDialog AutocompleteTextview that is also inside Main Activity to populate my textview insid main activity
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());
}
}
Data Base Helper
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;
}
projection that calls inside database helper
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";
}
}
When I Click The Text View Alert Dialog Pops Up
When I Type number The autocompletetextview will suggest data that has the same value from sqlite database table
But when I click an Item this Happens
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)
How can I create communication between my alert dialog class and main activity so i can use that method?