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

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

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

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

k3fezbri1#

@谢金是对的。您没有正确实现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, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_plus_event, container);
        setCancelable(false);

        return  view;
    }

    public void onClick(View view) {
        dismiss();
    }
}
wa7juj8i

wa7juj8i2#

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

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

相关问题