gpt4 book ai didi

java - NullPointerException 尝试获取 DialogFragment 的 PositiveButton

转载 作者:行者123 更新时间:2023-12-01 11:13:22 25 4
gpt4 key购买 nike

在我的 Android 应用程序中,我创建了一个带有自定义 View 的自定义 DialogFragment,但我想要具有透明背景的正面和负面按钮。

所以我看到了一些答案,我做了这样的事情:

private class PersonalInfoDialogFragment extends DialogFragment {

@Override
public void onStart(){
super.onStart();
Button pButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
Button nButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);

pButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
nButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view=inflater.inflate(R.layout.personalinformation_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.personalinformation)
.setView(view)
.setPositiveButton(R.string.edit, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Some code
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
final AlertDialog dialog = builder.create();
//I also tried this: dialog.getButton(DialogInterface.BUTTON_POSITIVE).setBackgroundColor(Color.TRANSPARENT);
return dialog;
}
}

我不明白为什么我会遇到 NullPointerException。有什么想法吗?

最佳答案

    public Button  pButton ,nButton ;

@Override
public void onStart(){
super.onStart();

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

LayoutInflater inflater = getActivity().getLayoutInflater();
final View view=inflater.inflate(R.layout.personalinformation_dialog, null);
pButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
nButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);

pButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
nButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.personalinformation)
.setView(view)
.setPositiveButton(R.string.edit, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Some code
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
final AlertDialog dialog = builder.create();
//I also tried this: dialog.getButton(DialogInterface.BUTTON_POSITIVE).setBackgroundColor(Color.TRANSPARENT);
return dialog;
}
}

关于java - NullPointerException 尝试获取 DialogFragment 的 PositiveButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32119998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com