tried to understand how RecyclerView works, but can't get done with code.
It dosn't show any error, but when I launch app, it automaticaly crashes.
In logcat i got error :
11-21 13:01:19.058 11109-11109/com.crelix.crelix E/AndroidRuntime: FATAL EXCEPTION: main Process: com.crelix.crelix, PID: 11109 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.crelix.crelix/com.crelix.crelix.NavigationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517) at android.app.ActivityThread.access$800(ActivityThread.java:162) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:189) at android.app.ActivityThread.main(ActivityThread.java:5529) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference at com.crelix.crelix.NavigationActivity.onCreate(NavigationActivity.java:78) at android.app.Activity.performCreate(Activity.java:5966) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517) at android.app.ActivityThread.access$800(ActivityThread.java:162) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:189) at android.app.ActivityThread.main(ActivityThread.java:5529) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
My fragment_market.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MarketScroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="RefreshMarket">
<android.support.v7.widget.CardView
android:id="@+id/MarketView"
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_margin="5dp">
<RelativeLayout
android:background="#fff"
android:elevation="4dp"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="120dp">
<ImageView
android:id="@+id/MarketImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/MarketTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:text=""
android:textSize="8sp"
android:layout_below="@+id/MarketImage"
android:layout_centerHorizontal="true" />
<Button
android:text="Sell"
android:layout_width="50dp"
android:layout_height="35dp"
android:id="@+id/MarketButton"
android:textSize="10sp"
android:layout_below="@+id/MarketPrice"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/MarketCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Count: "
android:textColor="#8b8b8b"
android:textSize="9sp"
android:layout_below="@+id/MarketTitle"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/MarketPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price: "
android:textColor="#8b8b8b"
android:textSize="9sp"
android:layout_below="@+id/MarketCount"
android:layout_centerHorizontal="true" />
</RelativeLayout>
My MarketHolder.java
package com.crelix.crelix;
public class MarketHolder {
private String title, count, price;
private int imageId;
public MarketHolder() {
}
public MarketHolder(int imageId, String title, String count, String price) {
this.imageId = imageId;
this.title = title;
this.count = count;
this.price = price;
}
public int getImage() {
return imageId;
}
public void setImage(int imageId) {
this.imageId = imageId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
My MarketAdapter.java
package com.crelix.crelix;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class MarketAdapter extends
RecyclerView.Adapter<MarketAdapter.MyViewHolder> {
private List<MarketHolder> MarketList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView imageId;
public TextView title, count, price;
public MyViewHolder(View view) {
super(view);
imageId = (ImageView) view.findViewById(R.id.MarketImage) ;
title = (TextView) view.findViewById(R.id.MarketTitle);
count = (TextView) view.findViewById(R.id.MarketCount);
price = (TextView) view.findViewById(R.id.MarketPrice);
}
}
public MarketAdapter(List<MarketHolder> MarketList) {
this.MarketList = MarketList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_market, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MarketHolder Market = MarketList.get(position);
holder.imageId.setImageResource(Market.getImage());
holder.title.setText(Market.getTitle());
holder.count.setText(Market.getCount());
holder.price.setText(Market.getPrice());
}
@Override
public int getItemCount() {
return MarketList.size();
}
}
And Main Activity where i get error on launc
private List<MarketHolder> MarketList = new ArrayList<>();
private RecyclerView recyclerView;
private MarketAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LoadFile();
setContentView(R.layout.activity_navigation);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
recyclerView = (RecyclerView) findViewById(R.id.MarketView);
mAdapter = new MarketAdapter(MarketList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
prepareMarketData();
private void prepareMarketData() {
MarketHolder Market = new MarketHolder(R.drawable.box, "Test", "Count: " +Part.AccessoriesPartsLVL1[0],"Price: " +Part.AccessoriesPartsLVL1[3]);
MarketList.add(Market);
Market = new MarketHolder(R.drawable.box, "Test2", "Count: " +Part.AccessoriesPartsLVL1[4],"Price: " +Part.AccessoriesPartsLVL1[7]);
MarketList.add(Market);
Market = new MarketHolder(R.drawable.box, "Test3", "Count: " +Part.AccessoriesPartsLVL1[8],"Price: " +Part.AccessoriesPartsLVL1[10]);
MarketList.add(Market);
mAdapter.notifyDataSetChanged();
}
Activity_Navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<include
layout="@layout/app_bar_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_navigation"
app:menu="@menu/activity_navigation_drawer" />