如何在片段内动态添加视图

时间:2016-07-07 11:53:23

标签: android android-fragments android-recyclerview android-view recycler-adapter

我正在制作RecyclerView inside a fragment。我通过Java代码创建了RecyclerView Adapter和LinearLayoutManager的对象。

public class RFPMenuFragment extends Fragment{

    List<MyRFP> myRFPList;
    RecyclerView recyclerViewRFP;
    RVAdapterRFP rvAdapterRFP;
    LinearLayoutManager llm;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.single_feed, container, false);

        recyclerViewRFP = (RecyclerView) view.findViewById(R.id.recycler_view_rfp);

        initializeData();


        rvAdapterRFP = new RVAdapterRFP(myRFPList);
        recyclerViewRFP.setAdapter(rvAdapterRFP);

        llm  = new LinearLayoutManager(getActivity());
        recyclerViewRFP.setLayoutManager(llm);

        return view;
}

但它在拥有setAdapter方法的行上给了我一个NullPointerException。即使我删除2个适配器行,我在setLayoutManager方法上也会出现此错误。我该如何解决这个问题?

这是我的LogCat:

                                                                       Process: com.example.h8pathak.jobs, PID: 26848
                                                                       java.lang.NullPointerException
                                                                           at com.example.h8pathak.jobs.rfp.RFPMenuFragment.onCreateView(RFPMenuFragment.java:41)
                                                                           at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
                                                                           at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
                                                                           at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
                                                                           at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
                                                                           at android.os.Handler.handleCallback(Handler.java:733)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:149)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5257)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                           at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

似乎您没有在single_feed布局中添加RecyclerView。

如果是这样,请在希望RecyclerView填充数据的布局中添加以下部分。

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

在您的情况下,请提及RFPMenuFragment.java中的布局,其中包含RecyclerView而不是single_feed布局(如果有,其名称暗示)。

答案 1 :(得分:0)

  1. 正如Drup Desai建议的那样,在设置适配器之前尝试设置布局管理器。

  2. 检查方法initializeData()是否实际上正在初始化并填充数据列表。当适配器连接到null时,RecyclerView列表可能会导致NPE。

答案 2 :(得分:-1)

onCreateView中,片段尚未附加到活动,因此getActivity()返回null。您需要在onActivityCreated()

中实例化布局管理器,适配器和recyclerview

编辑:代码

public class RFPMenuFragment extends Fragment{

List<MyRFP> myRFPList;
RecyclerView recyclerViewRFP;
RVAdapterRFP rvAdapterRFP;
LinearLayoutManager llm;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    //return super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.single_feed, container, false);
    recyclerViewRFP = (RecyclerView) view.findViewById(R.id.recycler_view_rfp);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    initializeData();

    rvAdapterRFP = new RVAdapterRFP(myRFPList);
    recyclerViewRFP.setAdapter(rvAdapterRFP);

    llm  = new LinearLayoutManager(getActivity());
    recyclerViewRFP.setLayoutManager(llm);
}