RecyclerView碰到的若干问题
RecyclerView控件非常好用,横向和纵向的滑动都能有很好的效果,而且view的重用,听说效率很高(不知道真实情况如何)。在使用的过程中遇到过几个bug,现在将bug list都贴出来,并分别说明每一种bug出现的原因以及其解决方法。
一、使用RecyclerView肯定要用到support-v7包,所以导入v7包,再直接在布局文件中使用该控件。
<android.support.v7.widget.RecyclerView android:id="@+id/channelmanager_recyclerview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="80dp" android:layout_marginTop="47dp" > </android.support.v7.widget.RecyclerView>
直接这样做的后果是很可能会产生这个RecyclerView无法解析的bug list:
08-28 15:41:12.982: E/AndroidRuntime(9259): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.recyclerviewtest/com.example.recyclerviewtest.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class android.support.v7.widget.RecyclerView 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.app.ActivityThread.access$800(ActivityThread.java:135) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.os.Handler.dispatchMessage(Handler.java:102) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.os.Looper.loop(Looper.java:136) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.app.ActivityThread.main(ActivityThread.java:5017) 08-28 15:41:12.982: E/AndroidRuntime(9259): at java.lang.reflect.Method.invokeNative(Native Method) 08-28 15:41:12.982: E/AndroidRuntime(9259): at java.lang.reflect.Method.invoke(Method.java:515) 08-28 15:41:12.982: E/AndroidRuntime(9259): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788) 08-28 15:41:12.982: E/AndroidRuntime(9259): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604) 08-28 15:41:12.982: E/AndroidRuntime(9259): at dalvik.system.NativeStart.main(Native Method) 08-28 15:41:12.982: E/AndroidRuntime(9259): Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class android.support.v7.widget.RecyclerView 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.view.LayoutInflater.createView(LayoutInflater.java:620) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 08-28 15:41:12.982: E/AndroidRuntime(9259): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.app.Activity.setContentView(Activity.java:1953) 08-28 15:41:12.982: E/AndroidRuntime(9259): at com.example.recyclerviewtest.MainActivity.onCreate(MainActivity.java:12) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.app.Activity.performCreate(Activity.java:5273) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 08-28 15:41:12.982: E/AndroidRuntime(9259): ... 11 more 08-28 15:41:12.982: E/AndroidRuntime(9259): Caused by: java.lang.reflect.InvocationTargetException 08-28 15:41:12.982: E/AndroidRuntime(9259): at java.lang.reflect.Constructor.constructNative(Native Method) 08-28 15:41:12.982: E/AndroidRuntime(9259): at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.view.LayoutInflater.createView(LayoutInflater.java:594) 08-28 15:41:12.982: E/AndroidRuntime(9259): ... 22 more 08-28 15:41:12.982: E/AndroidRuntime(9259): Caused by: java.lang.NoClassDefFoundError: android.support.v4.util.Pools$SimplePool 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.support.v7.widget.AdapterHelper.<init>(AdapterHelper.java:56) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.support.v7.widget.AdapterHelper.<init>(AdapterHelper.java:71) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.support.v7.widget.RecyclerView.initAdapterManager(RecyclerView.java:373) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:282) 08-28 15:41:12.982: E/AndroidRuntime(9259): at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:266) 08-28 15:41:12.982: E/AndroidRuntime(9259): ... 25 more 08-28 15:41:13.102: I/Process(9259): Sending signal. PID: 9259 SIG: 9
这个bug list产生的情况是最多的,也曾经让我不知道问题在哪里。仔细分析得知,是v7 jar包和v4 jar包的版本不同造成的,根据一个说法就是从官网同一个地方下载的v4和v7一般就不会有这样的问题。我这里有可以匹配的v4 和v7 jar,提供大家下载。用这两个jar基本可以保证不会出现此类的bug
二、RecyclerView没有进行设置setLayoutManager。这个问题就会直接导致空指针。
08-28 17:24:29.782: D/TextLayoutCache(11127): Using debug level = 0 - Debug Enabled = 0 08-28 17:24:30.292: D/AndroidRuntime(11127): Shutting down VM 08-28 17:24:30.292: W/dalvikvm(11127): threadid=1: thread exiting with uncaught exception (group=0x41676ba8) 08-28 17:24:30.322: D/dalvikvm(11127): GC_FOR_ALLOC freed 146K, 12% free 1372K/1548K, paused 12ms, total 12ms 08-28 17:24:30.322: E/AndroidRuntime(11127): FATAL EXCEPTION: main 08-28 17:24:30.322: E/AndroidRuntime(11127): Process: com.example.testrecycler, PID: 11127 08-28 17:24:30.322: E/AndroidRuntime(11127): java.lang.NullPointerException 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.support.v7.widget.RecyclerView.addFocusables(RecyclerView.java:1332) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.ViewGroup.addFocusables(ViewGroup.java:914) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.ViewGroup.addFocusables(ViewGroup.java:914) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.ViewGroup.addFocusables(ViewGroup.java:914) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.ViewGroup.addFocusables(ViewGroup.java:914) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.View.addFocusables(View.java:6711) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.FocusFinder.findNextFocus(FocusFinder.java:92) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.FocusFinder.findNextFocus(FocusFinder.java:65) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.ViewRootImpl.focusSearch(ViewRootImpl.java:5234) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.ViewRootImpl.leaveTouchMode(ViewRootImpl.java:3361) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.ViewRootImpl.ensureTouchModeLocally(ViewRootImpl.java:3294) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1262) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5672) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:772) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.Choreographer.doCallbacks(Choreographer.java:585) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.Choreographer.doFrame(Choreographer.java:555) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:758) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.os.Handler.handleCallback(Handler.java:733) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.os.Handler.dispatchMessage(Handler.java:95) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.os.Looper.loop(Looper.java:136) 08-28 17:24:30.322: E/AndroidRuntime(11127): at android.app.ActivityThread.main(ActivityThread.java:5017) 08-28 17:24:30.322: E/AndroidRuntime(11127): at java.lang.reflect.Method.invokeNative(Native Method) 08-28 17:24:30.322: E/AndroidRuntime(11127): at java.lang.reflect.Method.invoke(Method.java:515) 08-28 17:24:30.322: E/AndroidRuntime(11127): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788) 08-28 17:24:30.322: E/AndroidRuntime(11127): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604) 08-28 17:24:30.322: E/AndroidRuntime(11127): at dalvik.system.NativeStart.main(Native Method) 08-28 17:24:30.702: I/Process(11127): Sending signal. PID: 11127 SIG: 9
这个问题的解决方式就太简单了,初始化该RecyclerView(即findViewById来找到,然后设置setLayoutManager就行了)
public class MainActivity extends Activity { private RecyclerView mRecyclerView; private MyAdapter myAdaper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.mRecyclerView = (RecyclerView) this.findViewById(R.id.channelmanager_recyclerview); this.mRecyclerView.setHasFixedSize(true); this.mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); //this.myAdaper = new MyAdapter(); //this.mRecyclerView.setAdapter(myAdaper); }
如上adapter即使没有设置,也不会有任何bug问题,解决简单。
至此,基本上再没有关于RecyclerView的任何问题了…………
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-12 12:59:06