如何避免两次活动通信中出现空指针异常

问题描述 投票:0回答:2

[我正在尝试调用另一个类内的方法,当在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";
    }
}

enter image description here

当我单击“弹出文本视图警报”对话框时

enter image description here

当我键入数字时,autocompletetextview将建议与sqlite数据库表中的值相同的数据

enter image description here

但是当我单击一个项目时,会发生


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)

我如何在我的警报对话框类和主要活动之间建立通信,以便可以使用该方法?

android android-alertdialog
2个回答
0
投票

您打电话给findViewById太早了:

Account_No = findViewById(R.id.toprint_Account_No_Value);

这在构造活动时调用它。尝试用Account_No替换getotherinformationbyaccountforprinting中的findViewById(R.id.toprint_Account_No_Value),或在调用Account_No之后在onCreate中设置setContentView


0
投票

正如我在评论中建议的那样,您可以使用interface在Activity / Fragments / Adapters / etc`之间进行通信。

1。创建一个接口

public interface DataTransfer{
    void sendData(String data); //change parameter to whatever you want
}

2。在您的活动中实现界面]

3。在alertDialog中传递接口

ToPrintAccountSearchDialog toPrintAccountSearchDialog = 
    new ToPrintAccountSearchDialog(this);  
    // here this will pass the implemented interface

4。在对话框中,使用构造函数接收接口


private DataTransfer dataTransfer;

public ToPrintAccountSearchDialog(DataTransfer dataTransfer){
    this.dataTransfer = dataTransfer;
}

5。用户使用此dataTransfer将数据传递给活动

dataTransfer.sendData("Some important data");

6。在您的活动中,将有一个@Override方法void sendData(String data);

@Override
public void sendData(String data) {
    Log.e("TAG", "sendData: this data is from Dialog " + data);
}

编辑1:说明

有1个活动A和1个DialogFragment B

[B想要发送数据到A

[AB一个interface,所以当B调用dataTransfer.sendData("Hello");

A也将称为自己的overridden sendData(String data);

现在如果您打印此页

@Override
public void sendData(String data) {
    Log.e("TAG", "sendData: " + data);
}

将打印

Hello

您可以从method呼叫其他任何sendData

希望这会有所帮助!

请询问是否需要更多帮助

© www.soinside.com 2019 - 2024. All rights reserved.