在android studio中看不到对话框片段

ykejflvf  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(451)

我使用对话框片段来显示活动中的对话框。没有错误,我可以在logcat中找到日志,但在运行应用程序时看不到对话框。我不知道有什么问题。
这是我的活动代码:

  1. btnEventRegister.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. FragmentManager fragmentManager = getSupportFragmentManager();
  5. DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();
  6. Bundle bundle = new Bundle();
  7. bundle.putString("eventPlaceName", eventPlaceName);
  8. bundle.putLong("currentUserDistance", currentUserDistance);
  9. dialogPlusEvent.setArguments(bundle);
  10. dialogPlusEvent.show(fragmentManager, "DialogPlusEvent");
  11. Log.d("Dialog Fragment", "Working");
  12. }
  13. });

对话框片段代码:

  1. public class DialogPlusEvent extends DialogFragment implements View.OnClickListener{
  2. TextView dpeTvPlaceName, dpeStar, dpeTvMyDistance;
  3. private String dpePlaceName;
  4. private Long dpeMyDistance;
  5. private Button dpeBtnOkay;
  6. public static DialogPlusEvent getInstance() {
  7. DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();
  8. return dialogPlusEvent;
  9. }
  10. @Nullable
  11. public View OnCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  12. View view = inflater.inflate(R.layout.dialog_plus_event, container);
  13. SharedPreferences sharedPreferences = this.getContext().getSharedPreferences("file", Context.MODE_PRIVATE);
  14. currentPage = sharedPreferences.getString("currentPage", "");
  15. /.../
  16. Bundle bundle = getArguments();
  17. dpePlaceName = bundle.getString("eventPlaceName");
  18. dpeMyDistance = bundle.getLong("currentUserDistance");
  19. dpeBtnOkay = view.findViewById(R.id.dpeBtnOkay);
  20. dpeBtnOkay.setOnClickListener(this);
  21. setCancelable(false);
  22. return view;
  23. }
  24. public void onClick(View view) {
  25. dismiss();
  26. }
  27. }
k3fezbri

k3fezbri1#

@谢金是对的。您没有正确实现oncreateview方法。
我运行了你提供的代码,它工作得很好,我可以看到对话框,所以我认为问题不在这段代码。

  1. public class TestFragmentActivity extends AppCompatActivity implements View.OnClickListener {
  2. @Override
  3. protected void onCreate(@Nullable Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_test_fragment);
  6. findViewById(R.id.btn_click).setOnClickListener(this);
  7. }
  8. @Override
  9. public void onClick(View v) {
  10. FragmentManager fragmentManager = getSupportFragmentManager();
  11. DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();
  12. Bundle bundle = new Bundle();
  13. dialogPlusEvent.setArguments(bundle);
  14. dialogPlusEvent.show(fragmentManager, "DialogPlusEvent");
  15. Log.d("Dialog Fragment", "Working");
  16. }
  17. }
  1. public class DialogPlusEvent extends DialogFragment implements View.OnClickListener{
  2. TextView dpeTvPlaceName, dpeStar, dpeTvMyDistance;
  3. private String dpePlaceName;
  4. private Long dpeMyDistance;
  5. private Button dpeBtnOkay;
  6. public static DialogPlusEvent getInstance() {
  7. DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();
  8. return dialogPlusEvent;
  9. }
  10. @Nullable
  11. @Override
  12. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  13. View view = inflater.inflate(R.layout.dialog_plus_event, container);
  14. setCancelable(false);
  15. return view;
  16. }
  17. public void onClick(View view) {
  18. dismiss();
  19. }
  20. }
展开查看全部
wa7juj8i

wa7juj8i2#

你在执行 onCreateView 方法
把方法从 OnCreateViewonCreateView 如下所示:

  1. @Nullable
  2. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  3. View view = inflater.inflate(R.layout.dialog_plus_event, container);
  4. SharedPreferences sharedPreferences = this.getContext().getSharedPreferences("file", Context.MODE_PRIVATE);
  5. currentPage = sharedPreferences.getString("currentPage", "");
  6. /.../
  7. Bundle bundle = getArguments();
  8. dpePlaceName = bundle.getString("eventPlaceName");
  9. dpeMyDistance = bundle.getLong("currentUserDistance");
  10. dpeBtnOkay = view.findViewById(R.id.dpeBtnOkay);
  11. dpeBtnOkay.setOnClickListener(this);
  12. setCancelable(false);
  13. return view;
  14. }
展开查看全部

相关问题