在 Android Studio 中看不到对话框片段

时间:2021-04-23 00:19:30

标签: java android dialog fragment dialogfragment

我使用对话框片段来显示来自活动的对话框。 没有错误,我可以在我的 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();
    }
}

2 个答案:

答案 0 :(得分:0)

您在实施 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;
    }

答案 1 :(得分:0)

@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, @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();
    }
}
相关问题