ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决android - RecyclerView with Fragments

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:android Fragments:管理fragment

要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()。 你可以用FragmentManager来做以上事情:   1使用方法findF

I am trying to use the new RecyclerView widget inside a Fragment but I am getting this error:

Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MyActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference

What am I doing wrong?

This is my Fragment:

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public class RecyclerviewFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private RecyclerviewAdapter mRecyclerviewAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    private List<ViewModel> viewModel;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_recyclerview, container, false);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

        return rootView;
    }

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

        mRecyclerviewAdapter = new RecyclerviewAdapter(viewModel);
        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setAdapter(mRecyclerviewAdapter);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }
}

And my Activity:

推荐:Fragments In Android Studio

原英文链接:Fragments in Android Studio 这篇指南将会教你在Android Studio上简单地创建和使用Fragment,并在fragment里添加一个按钮,点击按钮会发送一个个动

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.List;

public class MyActivity extends Activity {

    private List<ViewModel> viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        RecyclerviewFragment recyclerviewFragment = new RecyclerviewFragment();
        getFragmentManager().beginTransaction().add(android.R.id.content, recyclerviewFragment).commit();

        viewModel.add(new ViewModel("View"));



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Thanks!

android android-fragments android-5.0-lollipop recyclerview
|
  this question
edited Dec 28 '16 at 16:52 Charuක 7,347 5 16 52 asked Jul 26 '14 at 18:59 ivoencarnacao 52 1 1 6 1   You have not created the object for viewModel and that's why the error. –  nikhil.thakkar Jul 26 '14 at 19:02 4   Please - you just had a silly Java programming problem where you forgot to initialize a variable. This does not help anybody (except you) Please remove this question as people end up here that really have problems with RecyclerViews and Fragments! Thank you for your cooperation! –  Zordid Dec 13 '14 at 13:17

 | 

1 Answers
1

解决方法

You didn't initialize private List<ViewModel> viewModel


|
  this answer
answered Jul 26 '14 at 19:15 Christopher Francisco 4,303 11 35 77

 | 

推荐:Android-Fragments的使用

请转原文学习: Using Fragmenys in Android - Tutorial where I found this resource:干干货分享——Android开发中学习资源大集合(译) 很素的翻译开始: 2. Fra


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。