AlertDialog 在片段中不起作用

Posted

技术标签:

【中文标题】AlertDialog 在片段中不起作用【英文标题】:AlertDialog not working in a Fragment 【发布时间】:2017-09-03 00:50:08 【问题描述】:

您好,我正在做一个应用程序,其中按下按钮并弹出一个带有 TimePicker 的 AlertDialog 窗口。但是它在 Fragment 中,所以 AppCompatActivity 不会这样做。

片段本身:

public class time_change extends Fragment implements  TimePickerFragment.TimeDialogListener 
private static final String DIALOG_TIME = "MainActivity.TimeDialog";

private View v;
private Button timePickerAlertDialog;
private ImageButton back;

@Override
@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    v = inflater.inflate(R.layout.pamokos, container, false);

    timePickerAlertDialog = (Button) v.findViewById(R.id.alert_dialog_time_picker);

    timePickerAlertDialog.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 

            TimePickerFragment dialog = TimePickerFragment.newInstance();
            dialog.show(getActivity().getFragmentManager(), "TimePickerFragment");
        
    );

    back = (ImageButton) v.findViewById(R.id.back_btn1);
    back.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 

            Pamokos fr = new Pamokos();
            time_change fr2 = new time_change();
            android.app.FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.activity_frag, fr);
            fragmentTransaction.remove(fr2);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        

    );
    return v;


@Override
public void onFinishDialog(String time) 
    Toast.makeText(getActivity(), "Selected Time : " + time, Toast.LENGTH_SHORT).show();


但它显示错误

dialog.show(getActivity().getFragmentManager(), "TimePickerFragment");

说“无法解析方法'show(android.app.FragmentManager, java.lang.String)'”

完整代码来自本教程https://www.androidtutorialpoint.com/basics/android-alert-dialog-tutorial-working-time-picker-date-picker-list-dialogs/

我很迷茫。

【问题讨论】:

【参考方案1】:

替换

TimePickerFragment dialog = TimePickerFragment.newInstance();
dialog.show(getActivity().getFragmentManager(), "TimePickerFragment");

有了这个

 FragmentManager fm = getActivity().getSupportFragmentManager();
 TimePickerFragment dialog = new TimePickerFragment ();
 dialog.show(fm, "TimePickerFragment");

【讨论】:

【参考方案2】:

添加 以下 在 DialogFragment 类中导入

import android.support.v4.app.DialogFragment;

替换

dialog.show(getActivity().getFragmentManager(), "TimePickerFragment");

dialog.show(getSupportFragmentManager(), "TimePickerFragment");

【讨论】:

我已经替换了它,但现在只有 getSupportFragmentManager() 变为全红色而不是红色下划线,它还显示“无法解析方法 'getSupportFragmentManager()'” 将 android.app.DialogFragment 替换为 import android.support.v4.app.DialogFragment; 请在 time_change 类中发布导入 导入android.app.FragmentManager;导入android.app.FragmentTransaction;导入 android.support.annotation.Nullable;导入android.app.Fragment;导入android.support.v4.app.DialogFragment;导入android.os.Bundle;导入 android.view.LayoutInflater;导入android.view.View;导入android.view.ViewGroup;导入android.widget.Button;导入android.widget.ImageButton;导入 android.widget.Toast; 尝试替换android.app.FragmentManager,导入android.app.FragmentTransaction;import android.app.Fragment;

以上是关于AlertDialog 在片段中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

onActivityResult 在片段中不起作用[重复]

Onclicklistener 在片段列表视图中不起作用

onRequestPermissionsResult 在片段中不起作用

Snackbar 在片段类中不起作用

BottomNavigation 在带有组件导航的片段中不起作用

listview OnItemClick 侦听器在片段中不起作用