Reputation: 11
I'm sorry if my english isn't perfect.
I am learning Java and Android Studio since 2 weeks, i'm trying to add sliding tabs, and inside a tab, a RecyclerView with CardViews as a row item having a name and a picture. My Sliding tabs works, and I can put a cardview in tabs, with a picture and a name. But when i use the RecyclerView, it doesn't work and i have an error. I can't resolve this problem since yesterday.
Here my program.
My Class Person
public class Person {
String name;
int photoId;
public Person(String name, int photoId) {
this.name = name;
this.photoId = photoId;
}
private List<Person> persons;
private void initializeData(){
persons = new ArrayList<>();
persons.add(new Person("Benjamin", R.drawable.team));
persons.add(new Person("Thomas", R.drawable.haltere));
}
//GETTER
public String getName() {
return this.name;
}
public int getPhoto() {
return this.photoId;
}
My Class Adapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cardView);
personName = (TextView)itemView.findViewById(R.id.cardTitle);
personPhoto = (ImageView)itemView.findViewById(R.id.cardPhoto);
}
}
List<Person> persons;
public RVAdapter(List<Person> persons){
this.persons = persons;
}
@Override
public int getItemCount() {
return persons.size();
}
@Override
public PersonViewHolder onCreateViewHolder (ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
return new PersonViewHolder(v);
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.personName.setText(persons.get(i).name);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
My Activity
public class ActivityHome extends AppCompatActivity {
// Declaring Your View and Variables
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"1","2","3","4"};
int Numboftabs =4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
RecyclerView rv = (RecyclerView)findViewById(R.id.my_recycler_view);
ArrayList<Person> persons = new ArrayList<>();
RVAdapter rAdapter = new RVAdapter(persons);
rv.setAdapter(rAdapter);
rv.setLayoutManager(layoutManager);
}
(There is a few code after, but it only uses for the Sliding Tabs, and it works.)
My recycler_view.xml
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android" />
My activity_home.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Tab.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/colorPrimary" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
></android.support.v4.view.ViewPager>
And my cardview.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="#ffffff"
app:cardCornerRadius="5dp"
app:cardElevation="7dp">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityHome"
android:background="#ffffff">
<TextView
android:id="@+id/cardTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Card Title"
android:textColor="#000000"
android:textSize="17sp" />
<ImageView
android:id="@+id/cardPhoto"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="50dp"
android:src="@drawable/haltere"
android:scaleType="center"
/>
</RelativeLayout>
My error :
Process: com.android.frutii.fitness5, PID:640 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.frutii.fitness5/com.android.frutii.fitness5.ActivityHome}: 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:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
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:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
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 com.android.frutii.fitness5.ActivityHome.onCreate(ActivityHome.java:45)
at android.app.Activity.performCreate(Activity.java:5975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
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:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
I have read a lot of answers on internet but cannot get rid of this error.
Upvotes: 1
Views: 1915
Reputation: 11
I move my RecyclerView.xml, i put the code in activity_home.xml like this :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<Tab.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android" />
</android.support.v4.view.ViewPager>
And now i'ts work partially, I have my 4 tabs, and I have a cardview too, but only one :( Why my second is not here ? Maybe this is not the good thing and I don't have to go in this direction.. ?
Upvotes: 0
Reputation: 5900
Activity.findViewById()
method is searching for my_recycler_view
inside the layout which was set with setContentView()
. So you should move RecyclerView
to R.layout.activity_home
layout.
Upvotes: 1
Reputation: 2405
Your RecyclerView
with Id R.id.my_recycler_view
does not exist in the activity_home layout. Use the R.layout.recycler
as layout the contentView in setContentView();
Upvotes: 0