选项卡式活动中的DialogFragment不会显示

时间:2016-08-30 08:11:34

标签: android android-studio android-fragments tabs

片段是选项卡式活动的一部分。我能够成功修改Android Studio 2.1.2的Tabbed Activity模板。现在我把它改成了它里面的所有碎片都有自己的FAB的地步。单击浮动操作按钮时,它应显示一个对话框。我按照官方文档的说明创建了一个DialogFragment。 FAB使用setOnClickListener来引用java文件中的方法。我无法使用

newEdQua.show(getSupportFragmentManager(), "createNewEdQua") 

显示对话框。它说它无法解决它

我还注意到它不会使用Stinrg数组的内容加载ListView。

总之,我想显示一个Fragment对话框(这是Tabbed Activity的一部分)。

我尝试清理和重建,使用项目同步gradle文件。我不想搞乱IDE配置文件。至少现在不能导致我进入一个非常重要的项目(因此禁止警告不是一种选择)。

这是我的代码。它仅包括有问题的区域。

对话片段(待显示):

public class createNewEdQua extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle saveSavedInstanceState){
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(R.string.createnewEdQua);

    builder.setMessage("New Qualification HERE!!");

    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });

    return builder.create();
}
}

片段(在选项卡式活动中作为选项卡放置):

public class edSET_Dashboard_Teachers extends Fragment {

FragmentManager fm;

//neInstance() method return reference to fragment
public static edSET_Dashboard_Teachers newInstance(){
    edSET_Dashboard_Teachers fragment = new edSET_Dashboard_Teachers();
    return fragment;
}


public edSET_Dashboard_Teachers() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

//following code is in public View onCreateView() method
    String[] edqua={"Conten Here","Contend Here","Content Here"};

//had to use this cause findViewById() method was not working as it should
    View view = inflater.inflate(R.layout.fragment_edset__dashboard__teachers, container, false);


    ArrayAdapter<String> listOfEdqua2 = new ArrayAdapter<String>(getContext(), R.layout.list_item_edqua, edqua);//i tried using this as the context but error

    ListView listView = (ListView)view.findViewById(R.id.edu_info);
    listView.setAdapter(listOfEdqua2);


//had to use setOnClickListener cause android:onClick in xml does not work with Fragments ive learned
    FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab_edset);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DialogFragment newEdQua = new createNewEdQua();
            newEdQua.show(getActivity().getFragmentManager(), "createNewEdQua");

            //addEdQua(view);
        }
    });


    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_edset__dashboard__teachers, container, false);

}

//the method to show the dialog
/*
public void addEdQua(View view){

}*/

}

当我在连接到我的计算机时在真实设备上运行它时,我点击FAB什么都没发生,没有崩溃没有任何错误。

2 个答案:

答案 0 :(得分:0)

首先声明一个FragmentManager变量:

FragmentManager fm;

然后初始化

fm = getFragmentManager();

然后在任何地方调用它..

createNewEdQua  dialogFragment = new createNewEdQua ();
dialogFragment.show(fm, "Your title");

答案 1 :(得分:0)

我想通了

return inflater.inflate(R.layout.fragment_edset__dashboard__teachers, container, false);

应该是

return view;//it already inflates the Fragment layout

您正在重新定义新视图