如何解决在 Android Studio 中看不到对话框片段
我使用对话框片段来显示来自活动的对话框。 没有错误,我可以在我的 Logcat 中找到 Log,但是当我运行应用程序时我看不到对话框。 我不知道有什么问题。
这是我的活动代码:
btnEventRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();
Bundle bundle = new Bundle();
bundle.putString("eventPlaceName",eventPlaceName);
bundle.putLong("currentUserDistance",currentUserDistance);
dialogPlusEvent.setArguments(bundle);
dialogPlusEvent.show(fragmentManager,"DialogPlusEvent");
Log.d("Dialog Fragment","Working");
}
});
对话框片段代码:
public class DialogPlusEvent extends DialogFragment implements View.OnClickListener{
TextView dpeTvPlaceName,dpeStar,dpeTvMyDistance;
private String dpePlaceName;
private Long dpeMyDistance;
private Button dpeBtnOkay;
public static DialogPlusEvent getInstance() {
DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();
return dialogPlusEvent;
}
@Nullable
public View OnCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_plus_event,container);
SharedPreferences sharedPreferences = this.getContext().getSharedPreferences("file",Context.MODE_PRIVATE);
currentPage = sharedPreferences.getString("currentPage","");
/.../
Bundle bundle = getArguments();
dpePlaceName = bundle.getString("eventPlaceName");
dpeMyDistance = bundle.getLong("currentUserDistance");
dpeBtnOkay = view.findViewById(R.id.dpeBtnOkay);
dpeBtnOkay.setOnClickListener(this);
setCancelable(false);
return view;
}
public void onClick(View view) {
dismiss();
}
}
解决方法
您在实施 onCreateView
方法时犯了一个错误
只需将方法从 OnCreateView
更改为 onCreateView
,如下所示:
@Nullable
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_plus_event,container);
SharedPreferences sharedPreferences = this.getContext().getSharedPreferences("file",Context.MODE_PRIVATE);
currentPage = sharedPreferences.getString("currentPage","");
/.../
Bundle bundle = getArguments();
dpePlaceName = bundle.getString("eventPlaceName");
dpeMyDistance = bundle.getLong("currentUserDistance");
dpeBtnOkay = view.findViewById(R.id.dpeBtnOkay);
dpeBtnOkay.setOnClickListener(this);
setCancelable(false);
return view;
}
,
@Shay Kin 是对的。您没有正确实现 onCreateView 方法。
我按照你提供的代码运行了,它运行良好,我可以看到对话框,所以我认为问题不在这段代码中。
public class TestFragmentActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_fragment);
findViewById(R.id.btn_click).setOnClickListener(this);
}
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();
Bundle bundle = new Bundle();
dialogPlusEvent.setArguments(bundle);
dialogPlusEvent.show(fragmentManager,"DialogPlusEvent");
Log.d("Dialog Fragment","Working");
}
}
public class DialogPlusEvent extends DialogFragment implements View.OnClickListener{
TextView dpeTvPlaceName,dpeStar,dpeTvMyDistance;
private String dpePlaceName;
private Long dpeMyDistance;
private Button dpeBtnOkay;
public static DialogPlusEvent getInstance() {
DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();
return dialogPlusEvent;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,container);
setCancelable(false);
return view;
}
public void onClick(View view) {
dismiss();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。