Flipboard / bottomsheet

Android component which presents a dismissible view from the bottom of the screen
BSD 3-Clause "New" or "Revised" License
4.53k stars 594 forks source link

java.lang.NullPointerException RecycyclerView #212

Open shriabhishekmishra opened 4 years ago

shriabhishekmishra commented 4 years ago

ERROR java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference

`package com.computerscienceknowledge.zaaruri;

import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;

import com.computerscienceknowledge.zaaruri.Model.Products; import com.computerscienceknowledge.zaaruri.Prevalent.Prevalent; import com.computerscienceknowledge.zaaruri.ViewHolder.ProductViewHolder; import com.firebase.ui.database.FirebaseRecyclerAdapter; import com.firebase.ui.database.FirebaseRecyclerOptions; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.navigation.NavigationView; import com.google.android.material.snackbar.Snackbar; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.squareup.picasso.Picasso;

import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; //import androidx.recyclerview.widget.RecyclerView; import de.hdodenhof.circleimageview.CircleImageView; import io.paperdb.Paper;

public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { DrawerLayout drawerLayout; ActionBarDrawerToggle toggle; Toolbar toolbar; NavigationView navigationView; View headerView; TextView userNameTextView; CircleImageView profileImageView; ProductViewHolder holder; View view; private DatabaseReference ProductsRef; private RecyclerView recyclerView; RecyclerView.LayoutManager layoutManager;

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

    ProductsRef = FirebaseDatabase.getInstance().getReference().child("Products");

    Paper.init(this);

    toolbar = findViewById(R.id.toolBar);
    toolbar.setTitle("Home");
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    drawerLayout =  findViewById(R.id.drawer);
    toggle = new ActionBarDrawerToggle(this, drawerLayout,toolbar,R.string.open,R.string.close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    headerView = navigationView.getHeaderView(0);
    userNameTextView = headerView.findViewById(R.id.user_profile_name);
    profileImageView = headerView.findViewById(R.id.user_profile_image);
    userNameTextView.setText(Prevalent.currentOnlineUser.getName());

    //   Picasso.get().load(Prevalent.currentOnlineUser.getImage()).placeholder(R.drawable.profile).into(profileImageView);

    recyclerView = findViewById(R.id.recycler_menu);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
}

@Override
protected void onStart()
{
    super.onStart();

    FirebaseRecyclerOptions<Products> options =
            new FirebaseRecyclerOptions.Builder<Products>()
                    .setQuery(ProductsRef, Products.class)
                    .build();

    FirebaseRecyclerAdapter<Products,ProductViewHolder> adapter =
            new FirebaseRecyclerAdapter<Products, ProductViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull Products model)
                {
                    holder.txtProductName.setText(model.getPname());
                    holder.txtProductDescription.setText(model.getDescription());
                    holder.txtProductPrice.setText("Price = " + model.getPrice() + "₹");
                    Picasso.get().load(model.getImage()).into(holder.imageView);
                }

                @NonNull
                @Override
                public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
                {
                    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items_layout, parent, false);
                    holder = new ProductViewHolder(view);
                    return holder;
                }
            };

    recyclerView.setAdapter(adapter);
    adapter.startListening();
}

@Override
public void onBackPressed() {
    drawerLayout = findViewById(R.id.drawer);
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    int id = item.getItemId();

/ if (id == R.id.action_settings) { return true; } / return super.onOptionsItemSelected(item); }

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)
{
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_cart)
    {

    }
    else if (id == R.id.nav_orders)
    {

    }
    else if (id == R.id.nav_categories)
    {

    }
    else if (id == R.id.nav_settings)
    {
        //  Intent intent = new Intent(HomeActivity.this, SettinsActivity.class);
        // startActivity(intent);
    }
    else if (id == R.id.nav_logout)
    {
        Paper.book().destroy();

        Intent intent = new Intent(HomeActivity.this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }

    DrawerLayout drawer =  findViewById(R.id.drawer);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

} `

content_home.xml

`<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".HomeActivity" tools:showIn="@layout/app_bar_main">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical">

</androidx.recyclerview.widget.RecyclerView>

`