简体   繁体   English

在片段onCreateView方法中该做什么?

[英]What to do in the Fragment onCreateView Method?

What I have are 3 Layouts. 我有3种布局。 fragment_tag , fragment_stunde and fragment_fach fragment_tagfragment_stundefragment_fach

fach should be in stunde and stunde in tag . fach应在stundestundetag

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.

相关问题 Android Fragment onCreateView() 方法中的容器是什么 - Android Fragment what container is it in onCreateView() method Android:屏幕旋转时,Fragment的onCreate()和onCreateView()应该做什么 - Android: on screen rotation what should Fragment's onCreate() and onCreateView() do Fragment的onCreateView()方法中的NullPointerException - NullPointerException in Fragment's onCreateView() method OnClickListener不在片段中工作(在onCreateView方法中) - OnClickListener Not Working in a Fragment (in onCreateView Method) 当popbackstack OnResume(),OnCreateView()时,什么都没有调用。 但是显示了先前的片段。 想恢复片段怎么办? - when popbackstack OnResume(), OnCreateView(), nothing is called. But the previous fragment is shown. want to resume fragment what to do? getActivity()在Fragment的onCreateView()方法中返回null - getActivity() returns null in onCreateView() method of Fragment 从片段的onCreateView方法向saveInstanceState添加变量 - adding variable to savedInstanceState from onCreateView method of fragment 如何从OnCreateView方法中获取片段的LayoutInflater - How to get a LayoutInflater for a fragment out of the OnCreateView method 在片段方法运行之前未调用onCreateView - onCreateView not called before method of fragment runs 片段中onCreateView方法之外的上下文更改为null - Context changes to null outside of onCreateView method in a fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM