RecyclerView因版本问题无法加载
前幾天在學習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.java3,如果我們現在加載進去,就會和上面報錯一樣~
而代碼應該是沒有問題的,我糾結了幾天,然后在網上看了好多分析
最后,在下面這個地方,會發現:
大體意思就是說,SDK的版本有問題,就是說版本有問題?我就想怎么會有問題呢?
我們回頭看第一個圖,添加依賴庫時候,我們添加的是v7:25.0.0 ? ?而appcompat的版本是v7:23.4.0,
RecylerView的版本比appcompat的版本高,是不支持的!
?
因此解決方法就很明顯了:
將appcompat的版本調整到一樣就好了,在添加依賴庫的界面將appcompat刪除掉,重新加一個高版本的。
這時候你會發現,其實添加的庫里面并沒有v7:23.4.0版本的,就很坑爹有沒有。這時候在運行就沒問題了~
轉載于:https://www.cnblogs.com/icyhusky/p/6080927.html
總結
以上是生活随笔為你收集整理的RecyclerView因版本问题无法加载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 性能测试篇 :Jmeter监控服务器性能
- 下一篇: 捷达vs7主机屏带dts功能吗?