OnCreateView 未在片段的对话框片段中调用

OnCreateView not called in a dialogfragment from a fragment

我有一个片段,它在单击按钮后执行或显示对话框片段。此对话框片段显示由代码生成的 table,但是当我尝试单击此按钮时会显示

当我在 LogCat 中跟踪它时,不会调用 onCreateView 对话框片段。有人可以帮我解释一下吗?我在 android 编程方面还不是很好,我知道我还有很多东西要学。

这里是调用对话片段的片段代码

public class fragment_schedule extends Fragment {

...............


public fragment_schedule(ArrayList<SubjSchedule> subj){
    subject = subj;
}
@SuppressWarnings("deprecation")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

..................

showlstbtn = (Button) rootView.findViewById(R.id.button_showlstschd);
        showlstbtn.setOnClickListener(new OnClickListener(){

            @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
            ft.addToBackStack(null);
            DialogFragment dialog = ShowLstDialog.newInstance(subject);
            dialog.show(ft, "dialog");
        }

    });

   ..........

这是我的对话片段

public class ShowLstDialog extends DialogFragment {

private static final String TAG = ShowLstDialog.class.getSimpleName();
public static Context mContext;
public static TableLayout tl;
public static TableRow trh;
private static ArrayList<SubjSchedule> subject= new ArrayList<SubjSchedule>(); 

public static DialogFragment newInstance(ArrayList<SubjSchedule> subj) {
    // TODO Auto-generated method stub
    DialogFragment f = new DialogFragment();
    subject = subj;
    Log.v(TAG, subject.size()+"");
    return f;
}



 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.v(TAG, "OnCreateView: " + subject.size()+"");
    View rootView = inflater.inflate(R.layout.fragment_dialog_showlst, container);
    //mEditText = (EditText) view.findViewById(R.id.txt_your_name);
    Log.v(TAG, "OnCreateView: " + subject.size()+"");
    getDialog().setTitle("");
    tl = (TableLayout) rootView.findViewById(R.id.tablelayout_schedlst);
    trh = new TableRow(getActivity());
    TextView[] tvh = new TextView[3];
    for(int i=0; i<3; i++){
        tvh[i] = new TextView(getActivity());
        tvh[i].setBackgroundResource(R.drawable.green2);
        tvh[i].setTextColor(getResources().getColor(R.color.LightCyan));
        tvh[i].setGravity(Gravity.CENTER);
        tvh[i].setPadding(30, 3, 30, 3);
        //tvh[i].setLayoutParams(params);
        trh.addView(tvh[i]);

    }

    tvh[0].setText("Subject");
    tvh[1].setText("Description");
    tvh[2].setText("Instructor");

    TableRow[] tr = new TableRow[subject.size()];
    for(int i=0; i<subject.size();i++){
        tr[i] = new TableRow(getActivity());
        TextView[] tv1 = new TextView[3];


        for(int k=0; k<3; k++){
            tv1[k] = new TextView(getActivity());
            tv1[k].setBackgroundResource(R.drawable.btn_default_disabled_holo_dark);
            tv1[k].setTextColor(getResources().getColor(R.color.list_background_pressed));
            tv1[k].setGravity(Gravity.CENTER);
            tv1[k].setPadding(30, 3, 30, 3);
            //tvh[i].setLayoutParams(params);
            tr[i].addView(tv1[i]);

        }

        tv1[0].setText(subject.get(i).getcn());
        tv1[1].setText(subject.get(i).getd());
        tv1[2].setText(subject.get(i).geti());
        tl.addView(tr[i]);

    }
    return rootView;
}



}

这里是对话框片段的xml文件

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@color/LightCyan"
        android:scrollbars="vertical" >

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal" >

                <TableLayout
                    android:id="@+id/tablelayout_schedlst"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:stretchColumns="*" >
                </TableLayout>
            </RelativeLayout>
        </HorizontalScrollView>
    </ScrollView>

</RelativeLayout>

在此先感谢您。

改变这个

public static DialogFragment newInstance(ArrayList<SubjSchedule> subj) {
    // TODO Auto-generated method stub
    DialogFragment f = new DialogFragment();
    subject = subj;
    Log.v(TAG, subject.size()+"");
    return f;
}

public static ShowLstDialog newInstance(ArrayList<SubjSchedule> subj) {
    // TODO Auto-generated method stub
    ShowLstDialog f = new ShowLstDialog();
    subject = subj;
    Log.v(TAG, subject.size()+"");
    return f;
}

并阅读

http://developer.android.com/reference/android/app/DialogFragment.html

Instead of (or in addition to) implementing onCreateView(LayoutInflater, ViewGroup, Bundle) to generate the view hierarchy inside of a dialog, you may implement onCreateDialog(Bundle) to create your own custom Dialog object

所以你应该使用:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)

替换:

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

而不是

public static DialogFragment newInstance(...) {
    DialogFragment f = new DialogFragment();
    return f;
}

public static DialogFragment newInstance(...) {
    DialogFragment f = new ShowLstDialog();
    return f;
}

也可以使用Bundle和setArguments、getArguments来传递参数。