Je fais un exemple d'application pour afficher la liste des tickets (Ticket a 2 Textviews et 1 Image View). Premièrement, j'ai essayé avec la vue de liste cela a fonctionné ... Mais le défilement était très moins réactif, donc je pensé à passer à Recycler View.En voyant des tutoriels et des échantillons, je suis venu avec le code ci-dessous.Bien que la construction Gradle est le succès, mais l'application se bloque .... Quelqu'un peut-il m'aider .. !! ??Recycleur View Ne répond pas
Cette activité est Mon activité dans laquelle je veux voir ma vue Recycleur.
public class Menu_activity extends AppCompatActivity {
private List<DataModel> dataModelList = new ArrayList<>();
private DataModel mdataModel;
private Recycle_view mRecycleview;
RecyclerView recyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
RecyclerView.LayoutManager mlayoutManager = new
LinearLayoutManager(this);
recyclerView.setLayoutManager(mlayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mRecycleview);
prepareData();
}
C'est ma fonction de données
private void prepareData(){
DataModel dataModel = new DataModel("Mad Max: Fury Road", R.drawable.ic4, "Action & Adventure");
dataModelList.add(dataModel);
dataModel = new DataModel("Inside Out", R.drawable.ic5,"Animation, Kids & Family");
dataModelList.add(dataModel);
dataModel = new DataModel("Star Wars: Episode VII - The Force Awakens", R.drawable.ic6, "Action");
dataModelList.add(dataModel);
dataModel = new DataModel("Shaun the Sheep", R.drawable.ic7, "Animation");
dataModelList.add(dataModel);``
dataModel = new DataModel("The Martian", R.drawable.app_icon, "Science Fiction & Fantasy");
dataModelList.add(dataModel);
dataModel = new DataModel("Mission: Impossible Rogue Nation", R.drawable.iconic, "Action");
dataModelList.add(dataModel);
mdataModel.notify();
Ceci est mon recycleur Voir Adaptateur
public class Recycle_view extends
RecyclerView.Adapter<Recycle_view.MyViewHolder> {
private List<DataModel> dataModelList;
public Recycle_view(List<DataModel> dataModelList){
this.dataModelList = dataModelList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_icy_ticket, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
DataModel dataModel = dataModelList.get(position);
holder.heads.setText(dataModel.getHead());
holder.descs.setText(dataModel.getDesc());
holder.images.setImageResource(dataModel.getImage());
}
@Override
public int getItemCount() {
return dataModelList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView heads,descs;
public ImageView images;
public MyViewHolder(View itemView) {
super(itemView);
heads = (TextView)itemView.findViewById(R.id.textView_head);
descs = (TextView)itemView.findViewById(R.id.textView_desc);
images = (ImageView) itemView.findViewById(R.id.imageView4);
}
}
}
C'est MyData Modèle
public class DataModel {
private String head;
private String desc;
private int image;
public DataModel(String head, int image, String desc){
this.head=head;
this.desc=desc;
this.image=image;
}
public String getHead() {
return head;
}
public void setHead(String head) {
this.head = head;
}
public String getDesc() {
return desc;
}
public void setDesc(String head) {
this.desc = desc;
}
public int getImage(){
return image;
}
public void setImage(int image){
this.image = image;
}
}
Le présent rapport est crash
08-28 20:05:09.281 1700-1714/system_process W/ActivityManager: Force finishing activity com.example.mishr.icycandy/.Menu_activity
08-28 20:05:09.353 1700-3581/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
08-28 20:05:09.353 1700-3581/system_process D/OpenGLRenderer: Swap behavior 1
08-28 20:05:09.353 1700-3581/system_process W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-28 20:05:09.353 1700-3581/system_process D/OpenGLRenderer: Swap behavior 0
08-28 20:05:09.358 1700-3581/system_process D/EGL_emulation: eglCreateContext: 0x96f78b80: maj 2 min 0 rcv 2
08-28 20:05:09.375 1700-3581/system_process D/EGL_emulation: eglMakeCurrent: 0x96f78b80: ver 2 0 (tinfo 0x96618ad0)
08-28 20:05:09.388 1700-3581/system_process D/EGL_emulation: eglMakeCurrent: 0x96f78b80: ver 2 0 (tinfo 0x96618ad0)
08-28 20:05:09.792 1700-1716/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{cd3b7f u0 com.example.mishr.icycandy/.Menu_activity t37 f}
08-28 20:05:11.181 1700-1700/system_process W/WindowManager: Attempted to remove non-existing token: [email protected]
08-28 20:05:12.364 1378-1409/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 1978693 , only wrote 1826640
FATAL EXCEPTION: main
Process: com.example.mishr.icycandy, PID: 17572
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mishr.icycandy/com.example.mishr.icycandy.Menu_activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.lang.Object.notify()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.lang.Object.notify()' on a null object reference
at com.example.mishr.icycandy.Menu_activity.prepareData(Menu_activity.java:104)
at com.example.mishr.icycandy.Menu_activity.onCreate(Menu_activity.java:81)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
recyclerView.setAdapter (mRecycleview) est l'origine du problème selon moi, vous ne l'avez pas initialisez mRecyclerview –
Publier un journal de plantage – Yupi
privé Recycle_view mRecycleview; –