【问题标题】:Custom Dialog error 'android.text.Editable android.widget.EditText.getText()' on a null object reference空对象引用上的自定义对话框错误“android.text.Editable android.widget.EditText.getText()”
【发布时间】:2020-10-28 18:15:17
【问题描述】:

我创建了一个显示项目网格的自定义对话框。单击另一个选项时,它还将打开另一个带有 editText 字段的自定义对话框。我面临的问题是文本字段出现空对象错误。我找到了答案here,但是当我应用它时,结果还是一样。

基本上,在从另一个自定义对话框打开的自定义对话框中检索文本字段值时,应用程序会崩溃。这是我目前所拥有的:

onCreate()

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button donate = findViewById(R.id.button);

    donate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDonateDialog();
        }
    });
}

showDonateDialog()

//Donate dialog with grid options
private void showDonateDialog() {
    // Prepare grid view
    GridView gridView = new GridView(this);
    String[] donateAmount = getResources().getStringArray(R.array.donationAmount);

    List<String> donateList = new ArrayList<String>();
    Collections.addAll(donateList, donateAmount);

    gridView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, donateList));
    gridView.setNumColumns(3);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), "Thank you for your donation!", Toast.LENGTH_SHORT).show();
        }
    });

    // Set grid view to alertDialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(gridView);
    builder.setTitle("Donate");
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.setNeutralButton("Other amount", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            showCustomDonate();
        }
    });
    builder.setCancelable(false);
    builder.show();
}

showCustomDonate()

//Donate dialog for custom amount
private void showCustomDonate() {
    AlertDialog.Builder builderCustom = new AlertDialog.Builder(this);
    Context context = builderCustom.getContext();
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View view = layoutInflater.inflate(R.layout.custom_donate_amount, null, false);

    final EditText donateAmount = findViewById(R.id.editAmount);
    final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_NEGATIVE) {
                dialog.cancel();
            } else {
            String amount = donateAmount.getText().toString(); //Error null object reference here
            Toast.makeText(getApplicationContext(), "Thank you for donating " + amount, Toast.LENGTH_LONG).show();
            }
        }
    };

    builderCustom.setView(view)
                .setTitle("Custom Donation")
                .setCancelable(false)
                .setPositiveButton("OK", listener)
                .setNegativeButton("CANCEL", listener)
                .show();

}

日志

    E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.customdialog, PID: 12011
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
    at com.example.customdialog.MainActivity$5.onClick(MainActivity.java:94)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

【问题讨论】:

    标签: java android


    【解决方案1】:

    希望对你有用

    showCustomDonate()

    //Donate dialog for custom amount
    private void showCustomDonate() {
        AlertDialog.Builder builderCustom = new AlertDialog.Builder(this);
        Context context = builderCustom.getContext();
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.custom_donate_amount, null, false);
    
        final EditText donateAmount = findViewById(R.id.editAmount);
        final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == DialogInterface.BUTTON_NEGATIVE) {
                    dialog.cancel();
                } else {
                String amount = donateAmount.getText().toString(); //Error null object reference here
                Toast.makeText(getApplicationContext(), "Thank you for donating " + amount, Toast.LENGTH_LONG).show();
                }
            }
        };
    
        builderCustom.setView(view)
                    .setTitle("Custom Donation")
                    .setCancelable(false)
                    .setPositiveButton("OK", listener)
                    .setNegativeButton("CANCEL", listener)
                    .show();
    
    }
    

    在此行更改

    final EditText donateAmount = view.findViewById(R.id.editAmount);
    

    【讨论】:

    • 哦,我真粗心。非常感谢您指出这一点!现在效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多