0

I tried to implement a fragment with RecyclerView inside it. So far i test the code with empty fragment work with no problem. But when i try to implement the recyclerview, it always crashing the activity MainMenu saying that

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object at ugm.fznzz.findmycoffee.Fragment.HomeFragment.onCreateView(HomeFragment.java:42)' on a null object reference

Here's the code on fragment which the error said on line 42, but I have no idea what's wrong with that line as i reference it to the RecyclerView Object ID in xml correctly.

private RecyclerView recyclerView;
    private RecyclerViewAdapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    List<ItemMenu> listData;
    FirebaseDatabase FDB;
    DatabaseReference DBR;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_home, container, false);
        recyclerView = recyclerView.findViewById(R.id.menuItem);  //this is line 42
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        listData = new ArrayList<>();
        adapter = new RecyclerViewAdapter(listData);
        FDB = FirebaseDatabase.getInstance();
        GetDataFirebase();

        return v;
    }

The object menuItem is this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/grad1"
    tools:context="ugm.fznzz.findmycoffee.Fragment.HomeFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/menuItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:paddingTop="20dp"
        android:paddingBottom="30dp"
        />
</RelativeLayout>

Also this is the activity

    private DrawerLayout drawer;
    ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        if (savedInstanceState == null) {
            MenuItem item =  navigationView.getMenu().getItem(0);
            onNavigationItemSelected(item);
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.nav_home: {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
                menuItem.setChecked(true);
                break;
            }
            case R.id.nav_sort: {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SortFragment()).commit();
                menuItem.setChecked(true);
                break;
            }
            case R.id.nav_settings: {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SettingsFragment()).commit();
                menuItem.setChecked(true);
                break;
            }
            case R.id.nav_login: {
                Intent login = new Intent(MainMenu.this, Login.class);
                startActivity(login);
                break;
            }
            case R.id.nav_signup:
                Intent sign_up = new Intent(MainMenu.this, SignUp.class);
                startActivity(sign_up);
                break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return false;
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
  • 1
    `recyclerView = recyclerView.findViewById(...` – That should be `recyclerView = v.findViewById(...`. You want to find the `RecyclerView` in the `View` you inflated, not in the `RecyclerView` itself. Possibly just a typo. – Mike M. Nov 26 '19 at 03:25
  • 1
    actually its not a typo, its the recommendation from the Android Studio itself. But thanks a lot :) – Fauzan P Nov 26 '19 at 03:45

1 Answers1

2

You are trying to reference recylerView from recyclerView. You need to use root view of fragment i.e v

recyclerView = recyclerView.findViewById(R.id.menuItem); 

should be

recyclerView = v.findViewById(R.id.menuItem); 
Dhiraj Sharma
  • 4,364
  • 24
  • 25