前几天在学习RecyclerView时候,一直失败,各种加载不上。下面是错误信息

D/AndroidRuntime: Shutting down VM

E/AndroidRuntime: FATAL EXCEPTION: main

                  Process:examples.ouc.com.learnrv, PID: 3030

                  java.lang.NoSuchMethodError:No static method getDisplay(Landroid/view/View;)Landroid/view/Display; in classLandroid/support/v4/view/ViewCompat; or its super classes (declaration of'android.support.v4.view.ViewCompat' appears in/data/data/examples.ouc.com.learnrv/files/instant-run/dex/slice-com.android.support-support-v4-23.4.0_52b240d56765eb2183f5273fe09102c073fde4ef-classes.dex)

                      atandroid.support.v7.widget.RecyclerView.onAttachedToWindow(RecyclerView.java:2391)

                      atandroid.view.View.dispatchAttachedToWindow(View.java:14514)

                      atandroid.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2836)

                      atandroid.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)

                      atandroid.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)

                      at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)

                      atandroid.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)

                      atandroid.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)

                      atandroid.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1364)

                      atandroid.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)

                      atandroid.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)

                      atandroid.view.Choreographer$CallbackRecord.run(Choreographer.java:858)

                      atandroid.view.Choreographer.doCallbacks(Choreographer.java:670)

                      atandroid.view.Choreographer.doFrame(Choreographer.java:606)

                      atandroid.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)

                      atandroid.os.Handler.handleCallback(Handler.java:739)

                      atandroid.os.Handler.dispatchMessage(Handler.java:95)

                      atandroid.os.Looper.loop(Looper.java:148)

                      atandroid.app.ActivityThread.main(ActivityThread.java:5417)

                      atjava.lang.reflect.Method.invoke(Native Method)

                      atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

                      atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

 

先说一下我的步骤吧:

1,首先创建一个Activity,然后添加依赖库:

2,然后在代码中添加RecyclerView:

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     private RecyclerView rv;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8 
 9         rv = new RecyclerView(this);
10         setContentView(rv);
11 
12         //设置布局
13         rv.setLayoutManager(new LinearLayoutManager(this));
14         //添加内容
15         rv.setAdapter(new RecyclerView.Adapter() {
16             class ViewHolder extends RecyclerView.ViewHolder{
17                 //绑定子对象的视图
18                 private TextView tv;
19 
20                 public ViewHolder(TextView itemView) {
21                     super(itemView);
22                     tv=itemView;
23                 }
24 
25                 public TextView getTv() {
26                     return tv;
27                 }
28             }
29             //创建holder
30             @Override
31             public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
32                 return new ViewHolder(new TextView(parent.getContext()));
33             }
34 
35             //对holder的textView进行操作
36             @Override
37             //第一个参数是我们绑定的holder
38             //第二个是一个索引,我们的当前初始化的数据是哪一条
39             public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
40 
41                 //注意这里引入的ViewHolder类是我们上面创建的类,不要加错包
42                ViewHolder vh = (ViewHolder) holder;
43                 vh.getTv().setText("Item "+ position);
44             }
45 
46             //获取RecylerView子对象数量
47             @Override
48             public int getItemCount() {
49                 return 10;
50             }
51         });
52     }
53 }
MainActivity.java

3,如果我们现在加载进去,就会和上面报错一样~

  而代码应该是没有问题的,我纠结了几天,然后在网上看了好多分析

  最后,在下面这个地方,会发现:

大体意思就是说,SDK的版本有问题,就是说版本有问题?我就想怎么会有问题呢?

我们回头看第一个图,添加依赖库时候,我们添加的是v7:25.0.0    而appcompat的版本是v7:23.4.0,

RecylerView的版本比appcompat的版本高,是不支持的!

 

因此解决方法就很明显了:

将appcompat的版本调整到一样就好了,在添加依赖库的界面将appcompat删除掉,重新加一个高版本的。

这时候你会发现,其实添加的库里面并没有v7:23.4.0版本的,就很坑爹有没有。这时候在运行就没问题了~