【问题标题】:Android: find a Button in a DialogAndroid:在对话框中找到一个按钮
【发布时间】:2014-09-20 17:36:13
【问题描述】:

我有一个由我的 MainActivity 调用的 Dialog。在这个对话框里面有一个自定义按钮。当我尝试在我的 MainActivity 中获取该按钮时

Button createDateButton = (Button) findViewById(R.id.create_date_button);

始终为 Null(因此我稍后会收到 Nullpointer 异常)。我认为这与对话框内的按钮有关...

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

这里还有一些代码:

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

public class MainActivity extends Activity implements UseDateInParentActivityInterface
{
    FragmentManager fragmentManager = getFragmentManager();

    CreateDialogFragment createDialogFragment;
    DialogFragment datePicker;


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

        homeFragment = new HomeFragment();

        fragmentManager.beginTransaction().add(R.id.mainFrame, homeFragment).commit();
        findViewById(R.id.bottomButton_home).setBackgroundResource(R.drawable.ic_home);
    }

    public void openCreate(View view)
    {
        createDialogFragment = new CreateDialogFragment();
        createDialogFragment.show(fragmentManager, "TEST");

        updateSelectedButton(3);
    }


    public void pickDate(View v)
    {
        datePicker = new DatePickerFragment();
        datePicker.show(getFragmentManager(), String.format(getResources().getString(R.string.pickDate)));
    }

    public void dateForWhatever(int year, int month, int day)
    {
        DateFormatter dateFormatter = new DateFormatter();
        String date = dateFormatter.toStringDE(year, month, day);

        Button createDateButton = (Button) findViewById(R.id.create_date_button);
        if (createDateButton != null)
        {
            createDateButton.setText(date);
        }
        else
        {
            System.out.println("bloeder Button ist gleich null");
        }
    }
}

这是 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>

约翰

【问题讨论】:

  • 我更新了答案。

标签: java android nullpointerexception dialog null


【解决方案1】:

在你的CreateDialogFragment,修改你的onCreateDialog()

@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();
 }

问题是按钮位于Dialog 的视图层次结构中,而不是Activity 的视图层次结构中。如果你从你的活动中调用findViewById(),你总是会得到空值:层次结构存在于两个不同的窗口中。

这个 sn-p 获取对话框中按钮的句柄。它调用findViewById() 从您的对话框(不是活动)的根视图 并检索按钮。然后,您可以将其保存到参考以供以后使用或直接在那里设置点击侦听器。在此监听器中,您可以编写自己的逻辑,例如调用活动的方法。

【讨论】:

  • 它的工作原理,太棒了!!!!!!!你真的帮助了我,甚至了解它为什么不起作用。非常感谢!!
【解决方案2】:

试试这个: 按钮 createDateButton = (Button) rootView.findViewById(R.id.create_date_button);

这应该可以工作。

【讨论】:

  • rooView 到底是什么意思?
  • 查看 rootView = layoutInflater.inflate(R.layout.dialog_create, null); builder.setView(rootView);
  • 好吧,我对 Android 还不是很了解,我到底该把什么放在哪里,我现在很困惑
  • 能否请您在此处添加整个课程,以便我可以准确地告诉您要修改的内容?
  • 好吧,我的建议是以编程方式添加按钮,而不是在 XML 中硬编码并通过 findViewById 找到它。
猜你喜欢
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
相关资源
最近更新 更多