Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:在对话框中查找按钮_Java_Android_Nullpointerexception_Dialog_Null - Fatal编程技术网

Java Android:在对话框中查找按钮

Java Android:在对话框中查找按钮,java,android,nullpointerexception,dialog,null,Java,Android,Nullpointerexception,Dialog,Null,我有一个由我的MainActivity调用的对话框。在这个对话框中有一个自定义按钮。当我尝试在我的主活动中使用该按钮时 Button createDateButton = (Button) findViewById(R.id.create_date_button); 它总是空的(因此我以后会得到一个空指针异常)。我认为这与对话框中的按钮有关 该对话框扩展了DialogFragment。它在运行时作为对另一个按钮的反应在MainActivity内部调用。如果您需要更多信息,请告诉我 下面是更多的

我有一个由我的MainActivity调用的对话框。在这个对话框中有一个自定义按钮。当我尝试在我的主活动中使用该按钮时

Button createDateButton = (Button) findViewById(R.id.create_date_button);
它总是空的(因此我以后会得到一个空指针异常)。我认为这与对话框中的按钮有关

该对话框扩展了DialogFragment。它在运行时作为对另一个按钮的反应在MainActivity内部调用。如果您需要更多信息,请告诉我

下面是更多的代码:

MainActivity(部分原因是它有200多行)

这是DialogFragment Java类

public class CreateDialogFragment extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstance)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater layoutInflater = getActivity().getLayoutInflater();

        builder.setView(layoutInflater.inflate(R.layout.dialog_create, null))
                .setMessage(R.string.create)
                .setPositiveButton(R.string.create, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i)
                    {}
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i)
                    {}
                });

        return builder.create();
    }
}
对话框xml.File

<?xml version="1.0" encoding = "utf-8"?>
<RelativeLayout
    xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:tools = "http://schemas.android.com/tools"
    android:id = "@+id/dialog_create"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:background="@color/create_bg_1"
    tools:context = ".CreateDialogFragment"
    >

    <Button
        android:layout_width = "wrap_content"
        android:layout_height = "@dimen/textFieldHight"
        android:ems = "6"
        android:text = "@string/date"
        android:background="@color/white"
        android:id = "@+id/create_date_button"
        android:onClick = "pickDate"
        android:layout_below = "@id/dialog_create_name"
        />


</RelativeLayout>

约翰

试试这个: Button createDateButton=(Button)rootView.findViewById(R.id.create\u date\u Button)


这应该可以正常工作。

CreateDialogFragment
中,修改
onCreateDialog()

问题在于按钮位于
对话框的视图层次结构中,而不是
活动的视图层次结构中。如果从活动中调用
findViewById()
,则始终会得到空值:层次结构位于两个不同的窗口中


此代码段获取对话框中按钮的句柄。它从对话框(不是活动)的根视图调用
findViewById()
,并检索按钮。然后,您可以将其保存到引用以供以后使用,或者直接在其中设置单击侦听器。在此侦听器中,您可以编写自己的逻辑,例如调用活动的方法。

您对rooView的确切含义是什么?View rootView=LayoutFlater.inflate(R.layout.dialog\u create,null);builder.setView(rootView);好吧,我还不太喜欢Android,我应该把什么放在哪里?我现在很困惑,请你把整个类添加到这里,这样我就可以准确地告诉你要修改什么了,我的建议是以编程方式添加按钮,而不是用XML硬编码并通过findViewById查找。我更新了我的答案。它很有效,太棒了!!!!!你真的帮助了我,甚至让我明白了为什么它不起作用。谢谢!!
<?xml version="1.0" encoding = "utf-8"?>
<RelativeLayout
    xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:tools = "http://schemas.android.com/tools"
    android:id = "@+id/dialog_create"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:background="@color/create_bg_1"
    tools:context = ".CreateDialogFragment"
    >

    <Button
        android:layout_width = "wrap_content"
        android:layout_height = "@dimen/textFieldHight"
        android:ems = "6"
        android:text = "@string/date"
        android:background="@color/white"
        android:id = "@+id/create_date_button"
        android:onClick = "pickDate"
        android:layout_below = "@id/dialog_create_name"
        />


</RelativeLayout>
@Override
public Dialog onCreateDialog(Bundle savedInstance)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater layoutInflater = getActivity().getLayoutInflater();
    View root = layoutInflater.inflate(R.layout.dialog_create, null);

    builder.setView(root).setMessage(R.string.create);
    // ...

    Button createDateBtn = (Button) root.findViewById(R.id.create_date_button);
    // set click listener here ...

    return builder.create();
 }