Currently i'm working a project, where i need to implement a RecyclerView/CardView. From MainActivity i call fragments in a switch case, where all fragments work. Whenever i press on the Fragment CardViewTabelFragment (in the emulator), then i crash and get a NullPointerException. Everything in MainActivity is working. I hope my description of my problem lives up to your expectations, if not then i can elaborate and/or attach more code. Feedback is also appreciated, so that i be more throughout next time I post a question here.
This is the fragment i call, when i'm in MainActivity:
public class CardViewTabelFragment extends Fragment {
private RecyclerView recyclerView;
private RecyclerAdapter adapter;
public CardViewTabelFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate( R.layout.fragment_card_view_tabel, container, false );
recyclerView = (RecyclerView) view.findViewById( R.id.recycler_view );
adapter = new RecyclerAdapter();
recyclerView.setAdapter( adapter );
final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager( getActivity() );
((LinearLayoutManager) layoutManager).setOrientation( LinearLayoutManager.VERTICAL );
recyclerView.setLayoutManager( layoutManager ) ;
return view;
}
}
new RecyclerAdapter(); extends to another java class where code is as follows:
public class RecyclerAdapter extends RecyclerView.Adapter {
public static String[] title = new String[] {"Sensor 1", "Sensor 2"};
public static String[] beskrivelse = new String[] {"Temperatur & Humdity Sensorer", "Light Sensorer"};
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from( parent.getContext() ).inflate( R.layout.fragment_card_view_tabel, parent, false );
return new RecyclerViewHolder( view );
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((RecyclerViewHolder) holder).bindView( position );
}
@Override
public int getItemCount() {
return title.length;
}
private class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView mBeskrivelse;
private TextView mTitle;
public RecyclerViewHolder(View itemView) {
super( itemView );
mBeskrivelse = (TextView) itemView.findViewById( R.id.item_beskrivelse );
mTitle = (TextView) itemView.findViewById( R.id.item_title );
itemView.setOnClickListener( this );
}
@Override
public void onClick(View v) {
}
public void bindView(int position) {
mBeskrivelse.setText( beskrivelse[position] );
mTitle.setText( title[position] );
}
}
}
` My LogCat is as follows, where u can clearly see, that the Exception occurs at
recyclerView.setAdapter( adapter );
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.yusuf.cxweb, PID: 31207
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.example.yusuf.cxweb.CardViewTabelFragment.onCreateView(CardViewTabelFragment.java:39)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
* UPDATE *
XML FILE FOR fragment_card_view_tabel
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_container1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="16dp"
app:cardBackgroundColor="@color/colorGrass"
app:cardCornerRadius="4dp"
app:cardElevation="8dp">
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_gravity="center"
android:textColor="@color/colorWhite"
android:text="Sensor 1"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView
android:id="@+id/item_beskrivelse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Sensor 1"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@color/colorWhite"
android:textSize="14sp" />
</android.support.v7.widget.CardView>
</LinearLayout>
* LAST UPDATE *
To make this work, i had to change the layout in the inflater -> from "fragment_card_view_table" to "card_list", which is another xml file i have, where the recyclerview is in. Thanks a lot to "TheWanderer" who pointed me in the right direction!