在Android Fragment

时间:2016-10-13 13:45:20

标签: android android-studio android-fragments gridview

嗨,我对Android开发相当陌生,而且我正在努力使用片段。

在我的fragmentPageAdapter中我有4个案例:

public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    switch (arg0) {
        case 0:
            return new friFragment();
        case 1:
            return new freeFragment();
        case 2:
            return new mixedFragment();
        case 3:
            return new recFragment();
        default:
            break;
    }
    return null;
}

只要片段不包含任何代码,我就可以轻松地从一个片段滑动到另一个片段。但是,我想在其中一个片段中实现GridView(recFragment case 3)。我的第一次尝试是在onCreateView

中初始化适配器
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

getActivity().setContentView(R.layout.activity_rec);
myGrid = (GridView) getActivity().findViewById(R.id.recGrid);
myGrid.setAdapter(new recAdapter(getActivity().getApplicationContext(), items));
myGrid.setOnItemClickListener(this);

return  inflater.inflate(R.layout.activity_rec,container,false);

这适用于我没有收到任何错误的延伸但是案例2只显示了半秒钟,之后它跳转到案例3,此时不可能在片段之间滑动。

使用onAttach:

 public void onAttach(Context context) {
    super.onAttach(context);
    this.context = context;
    myGrid = (GridView) getActivity().findViewById(R.id.recGrid);
    myGrid.setAdapter(new recAdapter(getActivity().getApplicationContext(), items));
    myGrid.setOnItemClickListener(this);
}

产生这个致命异常:

  

java.lang.NullPointerException:尝试调用虚方法' void android.widget.GridView.setAdapter(android.widget.ListAdapter)'在null对象引用上                                                                                    在tryouts.demoapplication.recFragment.onAttach(recipesFragment.java:42)                                                                                    在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)                                                                                    在android.support.v4.app.BackStackRecord.ensureFragmentsAreInitialized(BackStackRecord.java:1077)                                                                                    在android.support.v4.app.BackStackRecord.beginTransition(BackStackRecord.java:1032)                                                                                    在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:658)                                                                                    在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)                                                                                    在android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)                                                                                    在android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)                                                                                    在android.support.v4.view.ViewPager.populate(ViewPager.java:1177)                                                                                    在android.support.v4.view.ViewPager.populate(ViewPager.java:1025)                                                                                    在android.support.v4.view.ViewPager $ 3.run(ViewPager.java:254)                                                                                    在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858)                                                                                    在android.view.Choreographer.doCallbacks(Choreographer.java:670)                                                                                    在android.view.Choreographer.doFrame(Choreographer.java:603)                                                                                    在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844)                                                                                    在android.os.Handler.handleCallback(Handler.java:739)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:148)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

经过数小时搜索google& stackoverflow我已经了解到问题出在上下文中。我认为解决方案将是onAttach,但正如你所看到的那样对我没有帮助。除非我当然误解了如何使用onAttach功能,这很可能就是这种情况。

因此,如果我对上下文的问题是正确的,我该如何获得正确的上下文?

或许我实现gridview的方法是错误的?如果是这样,您对如何在片段中创建gridview的正确实现有任何建议吗?

1 个答案:

答案 0 :(得分:2)

也许这可以帮到你

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

View rootView = inflater.inflate(R.layout.activity_rec,container,false);
// dont forget to initialise the items list

myGrid = (GridView)rootView.findViewById(R.id.recGrid);
myGrid.setAdapter(new recAdapter(rootView.getActivity,items));
myGrid.setOnItemClickListener(this);

return  rootView;