[英]What to do in the Fragment onCreateView Method?
What I have are 3 Layouts. 我有3种布局。
fragment_tag
, fragment_stunde
and fragment_fach
fragment_tag
, fragment_stunde
和fragment_fach
fach
should be in stunde
and stunde
in tag
. fach
应在stunde
和stunde
的tag
。
And in fach are some TextView
s I want to set the text. 还有一些我想设置文本的
TextView
。
So my code so far is: 所以到目前为止我的代码是:
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;
}
But this gives me an Error: 但这给我一个错误:
java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.LinearLayout
at de.MayerhoferSimon.Vertretungsplan.TagFragment.onCreateView(TagFragment.java:56)
So how do I have to change the code that that what I want to do happens? 那么,我该如何更改要执行的代码?
Try making the following changes: 尝试进行以下更改:
LinearLayout tag = (LinearLayout)inflater.inflate(R.layout.fragment_tag, container, true);
to: 至:
LinearLayout tag = (LinearLayout)(inflater.inflate(R.layout.fragment_tag, null)).findViewById(R.id.idOfTag);
Similarly: 类似地:
LinearLayout stunde = (LinearLayout) inflater.inflate(R.layout.fragment_stunde, tag, true);
to: 至:
LinearLayout stunde = (LinearLayout)(inflater.inflate(R.layout.fragment_stunde, null)).findViewById(R.id.idOfStunde);
And: 和:
LinearLayout fach = (LinearLayout) inflater.inflate(R.layout.fragment_fach, stunde, true);
to: 至:
LinearLayout fach = (LinearLayout)(inflater.inflate(R.layout.fragment_fach, null)).findViewById(R.id.idOfFach);
Change the LinearLayout
to a View like such: 将
LinearLayout
更改为如下所示的视图:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View tag = inflater.inflate(R.layout.fragment_tag, container, true);
//Everything else...
return tag;
}
Also I believe you can only inflate one view per onCreateView
otherwise the layouts will replace one another. 另外,我相信您只能为每个
onCreateView
一个视图,否则布局将彼此替换。 You need to have three separate fragments that each inflate its own view and call these fragments individually from the top parent activity. 您需要具有三个单独的片段,每个片段都扩大自己的视图,并从顶级父活动单独调用这些片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.