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

Posted

技术标签:

【中文标题】在 Android Studio 中看不到对话框片段【英文标题】:Can't see Dialog Fragment in Android Studio 【发布时间】:2021-07-17 05:10:58 【问题描述】:

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

【问题讨论】:

【参考方案1】:

@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();
    

【讨论】:

【参考方案2】:

你在实现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;
    

【讨论】:

@SoyoungKim 请检查onCreateView是否被调用 我该如何检查..?我可以用日志检查吗?抱歉问..我是编程的超级初学者.. 我查过了。而且我认为onCreateView 没有被调用。我可以找到 Activity 中的日志,但没有 DialogFragment 日志。 您在 DialogFragment 中是否有任何警告,例如“从未使用此方法”? 让我们continue this discussion in chat。

以上是关于在 Android Studio 中看不到对话框片段的主要内容,如果未能解决你的问题,请参考以下文章