我所拥有的是3个布局。
fragment_tag
,fragment_stunde
和fragment_fach
fach
应位于stunde
stunde
和tag
{/ 1}}。
在fach中有一些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)
那么如何更改我想要做的代码?
答案 0 :(得分: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);
答案 1 :(得分: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
给一个视图充气,否则布局会相互替换。您需要有三个单独的片段,每个片段都会为自己的视图充气,并从顶级父活动中单独调用这些片段。