I am beginner in android, I am trying to make simple recycler view in Android. But shows error on adapter class. Please help me to solve.
My Main activity:
recycle_view.java:
package h.safmical.swipe;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class recycle_view extends AppCompatActivity{
private RecyclerView recyclerView;
private recycle_view_adapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView= (RecyclerView) findViewById(R.id.recycleview);
adapter = new recycle_view_adapter(this,getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public static List<recycle_view_information> getData(){
List<recycle_view_information> data=new ArrayList<>();
int[] icons={R.drawable.my};
String[] titles={"Harsh"};
for (int i=0;i<icons.length && i<titles.length;i++){
recycle_view_information current=new recycle_view_information();
current.iconid=icons[i];
current.title=titles[i];
data.add(current);
}
return data;
}
}
Adapter for recycler view:
recycle_view_adapter.java:
package h.safmical.swipe;
import android.content.Context;
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.Collections;
import java.util.List;
public class recycle_view_adapter extends RecyclerView.Adapter<recycle_view_adapter.MyViewHolder> {
private LayoutInflater inflater;
List<recycle_view_information> data= Collections.emptyList();
public recycle_view_adapter(Context context,List<recycle_view_information> data){
inflater=LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.recycle_view_custom_row,parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
recycle_view_information current=data.get(position);
holder.title.setText(current.title);
holder.icon.setImageResource(current.iconid);
}
@Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView icon;
public MyViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.text);
icon= (ImageView) itemView.findViewById(R.id.image);
}
}
}
recycle_view_information.java:
package h.safmical.swipe;
public class recycle_view_information {
int iconid;
String title;
}
My xml files: recycle_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
recycle_view_custom_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:src="@mipmap/ic_launcher_round"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="dummy"
android:layout_toRightOf="@+id/image"
android:layout_marginTop="15dp"
/>
</RelativeLayout>
Error Logcat:
06-15 15:03:38.904 8891-8891/h.safmical.swipe E/AndroidRuntime: FATAL EXCEPTION: main
Process: h.safmical.swipe, PID: 8891
java.lang.RuntimeException: Unable to start activity ComponentInfo{h.safmical.swipe/h.safmical.swipe.recycle_view}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at h.safmical.swipe.recycle_view.onCreate(recycle_view.java:24)
at android.app.Activity.performCreate(Activity.java:6245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
答案 0 :(得分:1)
You miss this line in onCreate
setContentView(R.layout.activity_recyclerview);
答案 1 :(得分:0)
You have not set the view of your activity.
Write the following in your activity's onCreate
method
setContentView(R.layout.recycle_view.xml)