Android:使用自定义视图

时间:2016-12-05 09:56:40

标签: android nullpointerexception dialog views

我有一个使用AlertDialog.Builder类创建的对话框,并调用builder.setView(int resource)为其提供文本输入的自定义布局。

当用户点击OK时,我正在尝试从布局上的EditTexts中检索值,但是当调用findViewByID()时,我得到空引用。如果在调用setContentView()之前尝试加载View,那么在它周围读取它似乎会发生在其他地方。使用Builder我显然没有这样做,有没有办法检索视图或者我应该以不同的方式构建我的对话框?

下面的Java和堆栈跟踪:

// Set up the on click of the Button
    Button add = (Button) findViewById(R.id.manage_connections_add_button);
    add.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(ManageConnectedServicesActivity.this);
            builder.setTitle("Add Service");

            builder.setView(R.layout.component_sharing_service_dialogue);

           // Set up the buttons on the dialog
            builder.setPositiveButton("Add", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // Get the connected service url
                    EditText url = (EditText) findViewById(R.id.add_sharing_service_url); // This is the offending line

                    addConnectedService(url.getText().toString()); // Crashes here
                }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            });

            builder.show();
        }
    });

堆栈追踪:

12-05 09:54:40.825 1889-1889/uk.mrshll.matt.accountabilityscrapbook  E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: uk.mrshll.matt.accountabilityscrapbook, PID: 1889
                                                                                  java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                                                      at uk.mrshll.matt.accountabilityscrapbook.ManageConnectedServicesActivity$1$1.onClick(ManageConnectedServicesActivity.java:63)
                                                                                      at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:135)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

4 个答案:

答案 0 :(得分:2)

创建一个视图,其中膨胀xml文件,并在findViewById()

之前使用该视图
final View view = inflater.inflate(R.layout.schedule,null);
builder.setView(view);

final EditText edtSelectDate = (EditText) view.findViewById(R.id.edtSelectDate);

答案 1 :(得分:0)

检查此代码如何使用编辑文本来扩充自定义对话框。

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);

final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);

dialogBuilder.setTitle("Custom dialog");
dialogBuilder.setMessage("Enter text below");
dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //do something with edt.getText().toString();
    }
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //pass
    }
});
AlertDialog b = dialogBuilder.create();
b.show();

希望这有帮助。快乐的编码。如果你找到有用的话,请回答答案

答案 2 :(得分:0)

 myTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View dialogView = inflater.inflate(R.layout.dialog_forgot_password, null);**
                dialogBuilder.setCancelable(true);
                final EditText mobileEt;
                Button done;

                mobileEt = (EditText) dialogView.findViewById(R.id.mobile_et);
                done = (Button) dialogView.findViewById(R.id.done);
                dialogBuilder.setView(dialogView);
                final AlertDialog alertDialog = dialogBuilder.create();
                alertDialog.show();
                done.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mobileEt.getText().toString().isEmpty()) {
                            mobileEt.setError(getString(R.string.email_or_mobile));
                        } else {

                            alertDialog.dismiss();

                                                hitdApi(mobileEt.getText().toString());

                                        }
                                );

                                mobileEt .setText("Please fill the value");

                });
            }
        });
        }

答案 3 :(得分:0)

使用此代码我认为它满足您的要求

\r