0

我拥有的是 3 种布局。 fragment_tag,fragment_stundefragment_fach

fach应该是 instundestundein tag

实际上,TextView我想设置一些文本。

所以到目前为止我的代码是:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout tag = (LinearLayout)inflater.inflate(R.layout.fragment_tag, container, true);

        database = new Database(context);

        database.open();

        for (int j = 1; j < 12; j++) {
            LinearLayout stunde = (LinearLayout) inflater.inflate(R.layout.fragment_stunde, tag, true);
            String[][] vplanDaten = database.getVplan(Integer.valueOf(DateHelper.get(DateHelper.WEEK_OF_YEAR)), index + 1, j);

            for (int k = 0; k < vplanDaten.length; k++) {
                LinearLayout fach = (LinearLayout) inflater.inflate(R.layout.fragment_fach, stunde, true);

                TextView fach_lehrer = (TextView) fach.findViewById(R.id.textView_lehrer);
                TextView fach_fach = (TextView) fach.findViewById(R.id.textView_fach);
                TextView fach_raum = (TextView) fach.findViewById(R.id.textView_raum);

                fach_lehrer.setText(vplanDaten[k][0]);
                fach_fach.setText(vplanDaten[k][1]);
                fach_raum.setText(vplanDaten[k][2]);
            }
        }
        database.close();
        return tag;
    }

但这给了我一个错误:

 java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.LinearLayout
        at de.MayerhoferSimon.Vertretungsplan.TagFragment.onCreateView(TagFragment.java:56)

那么我该如何更改我想做的代码呢?

4

2 回答 2

1

尝试进行以下更改:

LinearLayout tag = (LinearLayout)inflater.inflate(R.layout.fragment_tag, container, true);

至:

LinearLayout tag = (LinearLayout)(inflater.inflate(R.layout.fragment_tag, null)).findViewById(R.id.idOfTag);

相似地:

LinearLayout stunde = (LinearLayout) inflater.inflate(R.layout.fragment_stunde, tag, true);

至:

LinearLayout stunde = (LinearLayout)(inflater.inflate(R.layout.fragment_stunde, null)).findViewById(R.id.idOfStunde);

和:

LinearLayout fach = (LinearLayout) inflater.inflate(R.layout.fragment_fach, stunde, true);

至:

LinearLayout fach = (LinearLayout)(inflater.inflate(R.layout.fragment_fach, null)).findViewById(R.id.idOfFach);
于 2013-07-22T14:38:44.207 回答
0

将其更改为LinearLayout这样的视图:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View tag = inflater.inflate(R.layout.fragment_tag, container, true);
       //Everything else...
    return tag;
}

此外,我相信您只能为每个视图膨胀一个视图,onCreateView否则布局将相互替换。您需要拥有三个独立的片段,每个片段都膨胀自己的视图,并从顶级父活动中单独调用这些片段。

于 2013-07-22T14:24:37.497 回答