DialogFragment使用时为空(Android Studio)

时间:2019-06-10 03:49:25

标签: android dialogfragment

我将一堆代码移到程序中的DialogFragment位置,突然对话框为空。

试图输入其他数据,例如Textviews等,但它们也没有显示,因此我发现问题在于我如何将Layout文件与DialogFragment代码耦合。

DialogFragment类:

public class NewAccDialog extends DialogFragment {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View mView = getActivity().getLayoutInflater().inflate(R.layout.new_acc_dialog, null);
        builder.setTitle(getActivity().getString(R.string.overview_newacctitle_txt));

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        final Spinner accTypeSpinner = mView.findViewById(R.id.new_acc_spinner);
        accTypeSpinner.setAdapter(adapter);

        // onclicks removed

        return builder.create();
    }
}

我在活动中如何使用它:

public void createNewAcc(View v) {
        Bundle args = new Bundle();
        args.putLong("customerId", customerId);
        args.putParcelable("customerObject", customer);
        DialogFragment newAccDialog = new NewAccDialog();
        newAccDialog.setArguments(args);
        newAccDialog.show(getSupportFragmentManager(), "new_acc_dialog");

    }

未显示任何错误消息,但这是我的对话框的样子: Screenshot of empty dialog][1

1 个答案:

答案 0 :(得分:1)

请为您的构建者设置mView作为内容:

....
....
builder.setView(mView)
return builder.create();