Home > Mobile >  O bosses look RecyclerView setLayoutManager error
O bosses look RecyclerView setLayoutManager error

Time:10-16

Just learn a few days of android development, Java is not good QAQ
Now in the school's big homework problems solving

Error: under 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

Part of the code:
 private LinearLayoutManager LayoutManager. 
Private void initview () {
//get RecyclerView instance
RecyclerView RecyclerView=the findViewById (R.i d.r ecycler);

LayoutManager=new LinearLayoutManager (this);
LayoutManager. SetOrientation (LinearLayoutManager. HORIZONTAL);
RecyclerView. SetLayoutManager (LayoutManager);
MyAdapter adapter=new MyAdapter (listpic, this);
//complete adapter set
RecyclerView. SetAdapter (adapter);

}


The complete code:
 
Package com. Xxca. Cherry. Fragments;

The import android. Graphics. Drawable. Drawable;
The import android. Support. The v4. App. Fragments;
The import android. Support. The v4. App. FragmentManager;
The import android. Support. The v4. App. FragmentTransaction;
The import android. Support. V7. App. AppCompatActivity;
The import android. OS. Bundle;
The import android. Support. V7. Widget. LinearLayoutManager;
The import android. Support. V7. Widget. OrientationHelper;
The import android. Support. V7. Widget. RecyclerView;
The import android. View. The view;
The import android. Widget. TextView;

The import com. Xxca. Cherry. R;
The import com. Xxca. Cherry. Entity. PIC;
The import com. Xxca. Cherry. RecycleView. MyAdapter;

Import the Java. Util. ArrayList;
Import the Java. Util. List;

Public class firstActivity extends AppCompatActivity implements the View. An OnClickListener {
TextView tvmain, tvfind tvme;
Fragments mainfragment, findfragment mefragment;
FragmentManager FragmentManager;
Private LinearLayoutManager LayoutManager.
Private List Listpic=new ArrayList<> (a);
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
The setContentView (R.l ayout. Activity_first);
FragmentManager=getSupportFragmentManager ();//get manager
Inittv ();//initialize
Tvmain. PerformLongClick ();//simulate click

//recyclerview
The getid ();
//set the RecyclerView

Initview ();
}

Private void initview () {
//get RecyclerView instance
RecyclerView RecyclerView=the findViewById (R.i d.r ecycler);

LayoutManager=new LinearLayoutManager (this);
LayoutManager. SetOrientation (LinearLayoutManager. HORIZONTAL);
RecyclerView. SetLayoutManager (LayoutManager);
MyAdapter adapter=new MyAdapter (listpic, this);
//complete adapter set
RecyclerView. SetAdapter (adapter);

}

Private void getid () {
PIC pic_1=new PIC (R.d rawable. Header_pic_ad1);
PIC pic_2=new PIC (R.d rawable. Header_pic_ad2);
Listpic. Add (pic_1);
Listpic. Add (pic_2);

}


Private void inittv () {
Tvmain=the findViewById (R.i which vmain);
Tvfind=the findViewById (R.i which vfind);
Tvme=the findViewById (R.i which vme);
Tvmain. SetOnClickListener (this);
Tvfind. SetOnClickListener (this);
Tvme. SetOnClickListener (this);
}

//hide
Private void hideall (FragmentTransaction FragmentTransaction)
{
If (mainfragment!=null) fragmentTransaction. Hide (mainfragment);
If (findfragment!=null) fragmentTransaction. Hide (findfragment);
If (mefragment!=null) fragmentTransaction. Hide (mefragment);
}

Private void setnormal ()
{
Drawable Drawable=getResources (). GetDrawable (R.d rawable. Nav_main_normal);
//the top left corner starting position, the new size
Drawable. SetBounds (0, 0, drawable getMinimumWidth (), drawable. GetMinimumHeight ());
//can be set in the icon, don't display set to null
Tvmain. SetCompoundDrawables (null, drawable, null, null);

Drawable drawable2=getResources (). GetDrawable (R.d rawable. Nav_find_normal);
//the top left corner starting position, the new size
Drawable2. SetBounds (0, 0, drawable2 getMinimumWidth (), drawable2. GetMinimumHeight ());
//can be set in the icon, don't display set to null
Tvfind. SetCompoundDrawables (null, drawable2, null, null);

//to get replacement picture
Drawable drawable3=getResources (). GetDrawable (R.d rawable. Nav_me_normal);
//the top left corner starting position, the new size
Drawable3. SetBounds (0, 0, drawable3 getMinimumWidth (), drawable3. GetMinimumHeight ());
//can be set in the icon, don't display set to null
Tvme. SetCompoundDrawables (null, drawable3, null, null);

}
@ Override
Public void onClick (View View) {
FragmentTransaction FragmentTransaction=fragmentManager. BeginTransaction ();//open transaction
//Toast. MakeText (firstActivity. This, "selected", Toast. LENGTH_LONG), show ();
The switch (the getId ())
{
Case R.i which vmain:
Setnormal ();
//to get replacement picture
Drawable Drawable=getResources (). GetDrawable (R.d rawable. Nav_main_click);
//the top left corner starting position, the new size
Drawable. SetBounds (0, 0, drawable getMinimumWidth (), drawable. GetMinimumHeight ());
//can be set in the icon, don't display set to null
Tvmain. SetCompoundDrawables (null, drawable, null, null);
Hideall (fragmentTransaction);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related