Android应用开发错误库(上)

作者:郭孝星
微博:郭孝星的新浪微博
邮箱:guoxiaoxingv@163.com
博客:http://blog.csdn.net/allenwells
Github:https://github.com/guoxiaoxing

一 用户界面

1.1 ViewPager

1.1.1 java.lang.IllegalArgumentException: pointerIndex out of range

问题分析

这是ViewPager自身对于多点触控处理的bug,该bug可以追溯到Android4.4。该问题会导致应用崩溃。根本原因在于没有调用 super.onInterceptTouchEvent(); 造成 mActivePointerIdactivePointerIndex的值不能正确获取。导致父类 onTouchEvent取值错误,最终 onTouchEvent不能正确执行。

解决方案

覆写 ViewPager 的 onTouchEvent 与 onInterceptTouchEvent 方法,catch 该异常即可,如下所示:

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        try {
            return super.onInterceptTouchEvent(event);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        try {
            return super.onTouchEvent(event);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }
        return false;
    }

参考链接
http://leybreeze.com/blog/?p=2891

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0501/2822.html

1.1.2 Error inflating class android.support.v7.widget.RecyclerView

原因分析

在Eclipse引入RecyclerView中可能会遇到这个报错,主要是android.support.v7及其以来的android.support.v4的版本问题导致的。还有个与之类似的报错Error inflating class android.support.v7.widget.CardView,也是引包的问题。

解决方案

对于Eclipse,可以根据官方指南进行导包,如下所说:

Adding libraries without resources

  1. Make sure you have downloaded the Android Support Library using the SDK Manager.
  2. Create a libs/ directory in the root of your application project.
  3. Copy the JAR file from your Android SDK installation directory (e.g., /extras/android/support/v4/android-support-v4.jar) into your application’s project libs/ directory.
  4. Right click the JAR file and select Build Path > Add to Build Path.

Adding libraries with resources

  1. Make sure you have downloaded the Android Support Library using the SDK Manager.
  2. Select File > Import.
  3. Select Existing Android Code Into Workspace and click Next.
  4. Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the appcompat project, browse to /extras/android/support/v7/appcompat/.
  5. Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.
  6. In the new library project, expand the libs/ folder, right-click each .jar file and select Build Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar and android-support-v7-appcompat.jar files to the build path.
  7. Right-click the library project folder and select Build Path > Configure Build Path.
  8. In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.
  9. Uncheck Android Dependencies.
  10. Click OK to complete the changes.

You now have a library project for your selected Support Library that you can use with one or more application projects.

Add the library to your application project:

  1. In the Project Explorer, right-click your project and select Properties.
  2. In the category panel on the left side of the dialog, select Android.
  3. In the Library pane, click the Add button.
  4. Select the library project and click OK. For example, the appcompat project should be listed as android-support-v7-appcompat.
  5. In the properties window, click OK.

Note: If you are using the android-support-v7-mediarouter support library, you should note that it depends on the android-support-v7-appcompat library. In order for the v7 mediarouter library to compile, you must import both library projects into your development workspace. Then follow the procedure above to add the v7 appcompat project as a library to the v7 mediarouter library project.

参考链接

http://www.cnblogs.com/tianzhijiexian/p/4067308.html

https://developer.android.com/tools/support-library/setup.html#libs-with-res

1.1.3 IllegalStateException: The application’s PagerAdapter changed the adapter’s content without calling PagerAdapter#notifyDataSetChanged

原因分析

ADT22以后对getCount()的检查更为严格,通常意义上我们会认为数据来了以后再notifyDataSetChanged(),实际上在任何会引起getCount()变化的地方都应该调用这个方法。

解决方案

在任何会引起etCount()变化的地方调用notifyDataSetChanged()方法。还有个点就是要注意你initData()和serAdapter()的时机。之前有个错误就是:在一个Fragment里onCreateView()里setAdapter(),在onResume()里initData(),应用重启再进之后崩掉。这样的情况有很多种,但总体的思路就是注意在任何会引起etCount()变化的地方调用notifyDataSetChanged()方法。

参考链接

http://blog.csdn.net/hlglinglong/article/details/34845519
http://blog.daimajia.com/2013/06/%E3%80%90android%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3%E3%80%91the-applications-pageradapter-changed-the-adapters-contents-without-calling-pageradapternotifydatasetchanged/

1.2 RecyclerView

1.2.1 recyclerview No adapter attached; skipping layout

原因分析

这个主要是因为setAdapter()需要在UI线程中进行,如果你在一个异步加载的CallBack()方法里setAdapter(),可能会因为不是UI线程而导致这个问题。

解决方案

先在UI线程里setAdapter(),只是初始化,先不要取数据,等到数据来了再notifyDataSetChanged()。

参考链接

http://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout

二 应用组件

2.1 Activity跳转黑屏、白屏或闪屏

原因分析

这是因为我们在styles文件里给Activity添加里跳转动画,而有些手机判断这个的时候会出问题,导致黑屏、白屏或闪屏。

解决方案

styles文件里动画置null


<style name="Theme" parent="android:Theme">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

    <style name="Animation">
        <item name="android:activityOpenEnterAnimation">@null</item>
        <item name="android:activityOpenExitAnimation">@null</item>
        <item name="android:activityCloseEnterAnimation">@null</item>
        <item name="android:activityCloseExitAnimation">@null</item>
        <item name="android:taskOpenEnterAnimation">@null</item>
        <item name="android:taskOpenExitAnimation">@null</item>
        <item name="android:taskCloseEnterAnimation">@null</item>
        <item name="android:taskCloseExitAnimation">@null</item>
        <item name="android:taskToFrontEnterAnimation">@null</item>
        <item name="android:taskToFrontExitAnimation">@null</item>
        <item name="android:taskToBackEnterAnimation">@null</item>
        <item name="android:taskToBackExitAnimation">@null</item>
    </style>

然后在AndroidManifest文件的activity添加主题android:theme=”@style/Theme”。

参考链接

http://blog.csdn.net/menglele1314/article/details/45841073

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值