当在片段中使用但不在活动中时,RecyclerView会崩溃

时间:2016-01-19 04:16:09

标签: android android-fragments android-recyclerview

我在一项活动中使用了recyclerview,现在决定在片段中使用它。现在我的代码在layoutmanager = new LinearLayoutManager (globalContext);崩溃,并带有 NullPointerException

我不知道为什么。正如我所说,它在活动中使用时效果非常好。

我正在使用Xamarin,但这确实没有任何区别。

我只是将代码的第一部分作为应用程序崩溃包含在到达其他部分之前。

private Context globalContext = null;
        RecyclerView recyclerview;
        RecyclerView.LayoutManager layoutmanager;
        NewsAdapter adapter;

        public LatestNewsFragment()
        {
            this.RetainInstance = true;
        }

        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.fragment_latestnews, null);

            // Get our RecyclerView layout:
            recyclerview = view.FindViewById<RecyclerView> (Resource.Id.recyclerView);

            return view;
        }

        public override async void OnActivityCreated(Bundle savedInstanceState)
        {
            base.OnActivityCreated(savedInstanceState);
            globalContext = this.Context;

            //............................................................
            // Layout Manager Setup:

            // Use the built-in linear layout manager:
            layoutmanager = new LinearLayoutManager (globalContext);

            // Plug the layout manager into the RecyclerView:
            recyclerview.SetLayoutManager (layoutmanager);

            //............................................................
            // Adapter Setup:

            // Create an adapter for the RecyclerView, and pass it the
            // data set to manage:

            var rest = new RestAccess();    
            var listofarticles = await rest.ListArticlesAsync("somesource", "1");
            adapter = new NewsAdapter (listofarticles, globalContext);

            // Register the item click handler (below) with the adapter:
            adapter.ItemClick += OnItemClick;

            // Plug the adapter into the RecyclerView:
            recyclerview.SetAdapter (adapter);
        }

        // Handler for the item click event:
        void OnItemClick (object sender, int position)
        {
            // Display a toast that briefly shows the enumeration of the selected photo:
            int photoNum = position + 1;
            Toast.MakeText(globalContext, "This is card number " + photoNum, ToastLength.Short).Show();
        }
    }

编辑:

所以我将OnCreateView更改为:

public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var view = inflater.Inflate(Resource.Layout.fragment_latestnews, null);

    // Get our RecyclerView layout:
    recyclerview = view.FindViewById<RecyclerView> (Resource.Id.recyclerView);

    // Plug the layout manager into the RecyclerView:
    recyclerview.SetLayoutManager (new LinearLayoutManager (Activity));

    return view;
}

仍在recyclerview.SetLayoutManager (new LinearLayoutManager (Activity));

获取nullpointer异常

编辑2:

变量recyclerview实际上是null。不知道为什么。

答案: 我的应用程序崩溃有两个原因:

1- layoutmanager必须在OnCreateView中定义,但我在OnActivityCreated中执行此操作。

2-我的片段布局文件不包含RecyclerView。

3 个答案:

答案 0 :(得分:2)

初始化LayoutManager后,您需要在onCreateView()设置RecyclerView,而不是设置onActivityCreated()

mRecyclerView =  (RecyclerView)v.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(newLinearLayoutManager(mRecyclerView.getContext()));   

答案 1 :(得分:1)

而不是this.Context使用getActivity()来获取Fragment类中的Context。变化:

globalContext = this.Context;

globalContext = getActivity();

答案 2 :(得分:1)

尝试使用getActivity()

layoutmanager = new LinearLayoutManager (getActivity());