Recycler View无响应

时间:2017-08-28 14:30:49

标签: android

我正在制作一个示例应用程序来显示票证列表(票证有2个文本视图和1个图像视图)。首先我尝试使用列表视图它有效...但滚动的响应性非常低,所以我想切换到Recycler View.By看到教程和样本,我提出了以下代码。虽然Gradle构建是成功但应用程序崩溃.... 谁能帮帮我.. !! ??

此活动是我想要我的Recycler View的活动。

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();
}

这是我的数据功能

    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();

这是我的Recycler View Adapter

 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);
   }
}
}

这是MyData Model

 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;
    }
}

这是崩溃报告

    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: android.os.Binder@cd37caa
    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) 

3 个答案:

答案 0 :(得分:0)

首先初始化mRecycleView并删除

mDataMode.notify();

并写

mRecycleView.nofityDataSetChanged();

并更改适配器类的代码

    public class Recycle_view extends 
    RecyclerView.Adapter<Recycle_view.MyViewHolder> {
   }
    @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);
   }


}
}

还有一件事就是使用像滑动一样的imageloader来快速滚动...

答案 1 :(得分:0)

此行说明了您遇到错误的位置:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void java.lang.Object.notify()'

所以你没有初始化你的mdataModel。你甚至需要那行代码吗? mdataModel.notify();的目的是什么?实际上你的代码中有几个问题。可能您会得到另一个异常,因为您没有初始化适配器类,并且您没有将数据传递给适配器。第二个在适配器类中缺少Context

private List<DataModel> prepareData() {
  // your code
  return dataModelList;
}
适配器类中的

Context context;

public Recycle_view(Context context,List<DataModel> dataModelList){
       this.context = context;
       this.dataModelList = dataModelList;
   }

返回Menu_activity

mRecycleview = new Recycle_view (this, prepareData());
recyclerView.setAdapter(mRecycleview);

答案 2 :(得分:0)

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.third_layout);

   //I guess you do not need mdatamodel please remove it in your prepareData() method if you feel you still need it 
   mdatamodel = new DataModel(head,image,desc);
   //define head, image and desc and use it above.
   prepareData(); // Your data model will be ready
   recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
   RecyclerView.LayoutManager mlayoutManager = new 
   LinearLayoutManager(this);
   recyclerView.setLayoutManager(mlayoutManager);
   recyclerView.setItemAnimator(new DefaultItemAnimator());
   mRecyclerview = new Recycle_View(dataModelList);
   recyclerView.setAdapter(mRecycleview); 
}