Android开发中遇到的bug(1)

前言

看到其他同学总结的开发中的bug,真的很受教。自己也应该向他们学习,“好记性不如烂笔头”,何况自己又很健忘。希望自己也能总结出一份不错的bug汇总。

正文

1 No toolchains found in the NDK toolchains folder for ABI with prefix: mipsel-linux-android

日期:2018年12月21日21:52:42
这是在 AS 编译项目时出现的错误,自己是通过搜索解决的。查看: 完美解决 No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android

2 Caused by: android.database.sqlite.SQLiteException: no such table: (code 1) Android

这个异常时因为数据库不存在导致的。在我的项目中,是因为assets 目录下的数据库没有解压到data/data/目录下导致的。

3 RecyclerView notifyItemChanged 没有效果

日期:2019年1月9日13:10:32
这是因为自己在子线程中调用此刷新导致的,需要在主线程中调用。

4 NotSerializableException 异常

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.wql.downloader.beans.TabInfo)
Caused by: java.io.NotSerializableException: com.wql.downloader.fragment.WebFragment
原因:这是因为在一个实现了 Serializable 接口的类中,存在未序列化的变量。
解决办法:使用 transient 关键字修饰该变量,阻止其被序列化。但最终的解决办法是不在这里保存 WebFragment 对象。

5 RecyclerView 刷新时的异常

01-10 22:08:22.746 26570-26570/com.wql.downloaderplay E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.wql.downloaderplay, PID: 26570
    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{5f6ec69 position=3 id=-1, oldPos=3, pLpos:-1 scrap [attachedScrap] tmpDetached no parent} android.support.v7.widget.RecyclerView{5b499f7 VFED..... .F....ID 0,171-1080,1590 #7f08014a app:id/recycler_view}, adapter:com.wql.downloader.adapters.DownloadProgressAdapter@d90064, layout:android.support.v7.widget.LinearLayoutManager@bdbd2cd, context:com.wql.downloader.activity.HomeActivity@629111a
        at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5715)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5898)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
        at android.view.View.layout(View.java:16754)
        at android.view.ViewGroup.layout(ViewGroup.java:5462)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
        at android.view.View.layout(View.java:16754)
        at android.view.ViewGroup.layout(ViewGroup.java:5462)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:338)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at android.view.View.layout(View.java:16754)
        at android.view.ViewGroup.layout(ViewGroup.java:5462)
        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
        at android.view.View.layout(View.java:16754)
        at android.view.ViewGroup.layout(ViewGroup.java:5462)
        at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:16754)
        at android.view.ViewGroup.layout(ViewGroup.java:5462)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:338)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at android.view.View.layout(View.java:16754)
        at android.view.ViewGroup.layout(ViewGroup.java:5462)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1764)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1607)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1516)
        at android.view.View.layout(View.java:16754)
        at android.view.ViewGroup.layout(ViewGroup.java:5462)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:338)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at android.view.View.layout(View.java:16754)
        at android.view.ViewGroup.layout(ViewGroup.java:5462)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1764)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1607)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1516)
        at android.view.View.layout(View.java:16754)
        at android.view.ViewGroup.layout(ViewGroup.java:5462)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:338)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2748)
        at android.view.View.layout(View.java:16754)
        at android.view.ViewGroup.layout(ViewGroup.java:5462)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2225)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
    	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:

待解决。

6 关于Activity切换动画(overridePendingTransition)的黑色背景问题

这是因为在 overridePendingTransition:

public void overridePendingTransition(int enterAnim, int exitAnim)

的第二个参数设置为 null。
解决办法是在第二个参数设置动画,但不会有任何的动画。

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:fromXDelta="0"
           android:toXDelta="0"
           android:fromYDelta="0"
           android:toYDelta="0"
           android:duration="250"/>

7 android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

时间:2019年1月16日13:20:51
使用非Activity的上下文startActivity 时,需要:

chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

8 分享文件时报错 FileUriExposedException:

android.os.FileUriExposedException: file:///storage/emulated/0/AwesomeDownload/Download/IMDb%20-%20Movies%2C%20TV%20and%20Celebrities%20-%20IMDb_1547706246.mp4 exposed beyond app through ClipData.Item.getUri()

时间:2019年1月17日17:07:12
这是在7.0以上手机上的报错,7.0开始不允许带有file://的Uri离开应用,要和其他应用共享File数据,需要使用content://的方式。
解决办法:适配7.0以上的手机。

public static boolean shareFile(Context context, File file) {
        if (file == null || !file.exists()) {
            Toast.makeText(MyApplication.getContext(), R.string.finish_popup_menu_share_no_file, Toast.LENGTH_SHORT).show();
            return false;
        }
        Intent share = new Intent(Intent.ACTION_SEND);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri uri = FileProvider.getUriForFile(context,
                    context.getApplicationContext().getPackageName() + ".fileprovider", file);
            share.putExtra(Intent.EXTRA_STREAM, uri);
        } else {
            share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        }
        share.setType(getMimeType(file.getAbsolutePath()));
        //  If you're using an intent to make the system open your file, you may need to add the following line of code
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        if (context.getPackageManager().resolveActivity(share, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            try {
                Intent chooser = Intent.createChooser(share, context.getString(R.string.finish_popup_menu_share));
                chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(chooser);
                return true;
            } catch (ActivityNotFoundException e) {
                return false;
            }
        }

        return false;
    }

在 xml 目录下,添加 storage_file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

在清单文件中添加:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <!--grantUriPermissions:true,表示授予 URI 临时访问权限。-->
    <!--exported:要求必须为false,为true则会报安全异常。-->
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/storage_file_paths" />
</provider>

9 在 API19的手机上使用 vector 资源报错

android.content.res.Resources$NotFoundException: File res/drawable/ic_book_24dp.xml from drawable resource ID #0x7f080080. If the resource you are trying to use is a vector resource, you may be referencing it in an unsupported way. See AppCompatDelegate.setCompatVectorFromResourcesEnabled() for more info.
        at android.content.res.Resources.loadDrawable(Resources.java:2152)
        at android.content.res.Resources.getDrawable(Resources.java:710)
        at com.wql.downloader.fragment.MyFragment.initView(MyFragment.java:72)
        at com.wql.downloader.fragment.MyFragment.onViewCreated(MyFragment.java:59)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:933)
        at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
        at android.content.res.Resources.loadDrawable(Resources.java:2148)
        at android.content.res.Resources.getDrawable(Resources.java:710) 
        at com.wql.downloader.fragment.MyFragment.initView(MyFragment.java:72) 
        at com.wql.downloader.fragment.MyFragment.onViewCreated(MyFragment.java:59) 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471) 
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784) 
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797) 
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625) 
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411) 
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366) 
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273) 
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733) 
        at android.os.Handler.handleCallback(Handler.java:808) 
        at android.os.Handler.dispatchMessage(Handler.java:103) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:5299) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641) 
        at dalvik.system.NativeStart.main(Native Method) 

时间:2019年1月19日11:02:31
解决办法:在Activity的基类中添加:

static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

参考:https://stackoverflow.com/questions/38467680/android-getting-resourcesnotfoundexception-for-abc-ic-ab-back-material

10 混淆反射导致的异常

01-22 09:48:05.434 8410-8410/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.wql.downloaderplay, PID: 8410
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wql.downloaderplay/com.wql.downloader.activity.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)
        at android.app.ActivityThread.access$900(ActivityThread.java:165)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:150)
        at android.app.ActivityThread.main(ActivityThread.java:5546)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
        at com.wql.downloader.views.BottomNavigationViewEx.getBottomNavigationItemViews(BottomNavigationViewEx.java:46)
        at com.wql.downloader.views.BottomNavigationViewEx.b(BottomNavigationViewEx.java:35)
        at com.wql.downloader.activity.HomeActivity.e(HomeActivity.java:746)
        at com.wql.downloader.activity.HomeActivity.r(HomeActivity.java:163)
        at com.wql.downloader.activity.HomeActivity.onCreate(HomeActivity.java:136)
        at android.app.Activity.performCreate(Activity.java:6367)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2397)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504) 
        at android.app.ActivityThread.access$900(ActivityThread.java:165) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:150) 
        at android.app.ActivityThread.main(ActivityThread.java:5546) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) 

时间:2019年1月22日22:38:20
原因:使用 BottomNavigationView 进行扩展,得到如下类:

public class BottomNavigationViewEx extends BottomNavigationView {

    public BottomNavigationViewEx(Context context) {
        super(context);
    }

    public BottomNavigationViewEx(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BottomNavigationViewEx(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private BottomNavigationItemView[] mButtons;
    private BottomNavigationMenuView mMenuView;

    public BottomNavigationItemView getBottomNavigationItemView(int position) {
        return getBottomNavigationItemViews()[position];
    }

    private BottomNavigationItemView[] getBottomNavigationItemViews() {
        if (null != mButtons)
            return mButtons;
        /*
         * 1 private final BottomNavigationMenuView mMenuView;
         * 2 private BottomNavigationItemView[] mButtons;
         */
        BottomNavigationMenuView mMenuView = getBottomNavigationMenuView();
        mButtons = getField(mMenuView.getClass(), mMenuView, "buttons");
        return mButtons;
    }

    /**
     * get private mMenuView
     *
     * @return
     */
    private BottomNavigationMenuView getBottomNavigationMenuView() {
        if (null == mMenuView)
            mMenuView = getField(BottomNavigationView.class, this, "menuView");
        return mMenuView;
    }

    /**
     * get private filed in this specific object
     *
     * @param targetClass
     * @param instance    the filed owner
     * @param fieldName
     * @param <T>
     * @return field if success, null otherwise.
     */
    private <T> T getField(Class targetClass, Object instance, String fieldName) {
        try {
            Field field = targetClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            return (T) field.get(instance);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}

但是在打 release 包时,没有添加混淆文件,导致其中需要取反射的字段 buttons,menuView,无法找到。最终导致空指针。
添加混淆文件,解决了这个问题:

-keep public class android.support.design.widget.BottomNavigationView { *; }
-keep public class android.support.design.internal.BottomNavigationMenuView { *; }
-keep public class android.support.design.internal.BottomNavigationItemView { *; }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: Android底层开发指的是在Android操作系统对底层系统组件进行开发和调试的过程。底层开发需要对操作系统的内部机制、底层接口和系统调用有深入的了解。 在Android底层开发,需要掌握Linux内核、C/C++编程语言以及JNI(Java Native Interface)等技术。开发者需要了解Linux内核的机制和原理,以便能够理解和分析Android系统在底层运行时的行为和逻辑。 在底层开发,还需要通过C/C++编程语言来编写底层库和驱动程序,与硬件进行交互和通信。这些底层库和驱动程序负责实现Android系统各个组件的底层功能,如图形显示、音频处理、网络通信等。 另外,JNI技术用于在Java层和底层库之间进行交互。通过JNI,可以调用底层库的函数和方法,实现Java层与底层的数据传递和功能调用。 底层开发经常会涉及到调试和性能优化的工作。开发者需要使用调试工具来分析和追踪底层代码的执行过程,以及解决底层开发遇到的问题和bug。性能优化方面,可以通过调整底层代码和参数来提高系统的性能和响应能力。 总之,Android底层开发是一项需要对操作系统原理和底层技术有深入了解的工作。通过学习和实践,开发者可以掌握底层开发技术,为Android系统的功能和性能做出贡献。 ### 回答2: Android底层开发是指在Android操作系统直接与硬件进行交互的开发工作。在Android底层开发开发者需要熟悉Linux内核、硬件驱动程序以及底层库等关键技术。 Android操作系统基于Linux内核,因此熟悉Linux内核是进行Android底层开发的基础。开发者需要了解Linux内核的基本原理和结构,以便理解Android系统底层的运行机制。 硬件驱动程序是Android底层开发的重要组成部分,它们负责将硬件设备与Android系统进行连接和通信。开发者需要掌握硬件驱动的编写和调试技巧,以确保硬件设备在Android系统能够正常工作。 底层库是Android底层开发的另一个关键技术。Android提供了一系列的底层库,用于实现底层功能,比如图形处理、网络通信、多媒体播放等。开发者需要熟悉这些库的使用方法和原理,以便在底层开发进行功能的实现和调试。 Android底层开发通常涉及到一些高级的编程语言,比如C/C++。开发者需要熟练掌握这些编程语言,以便能够编写出高效和稳定的底层代码。 总之,Android底层开发是一项复杂而庞大的工作,需要开发者具备扎实的技术基础和深厚的理论知识。只有掌握了底层开发所需的关键技术,开发者才能够在Android系统开发出高效、稳定和功能丰富的应用程序。 ### 回答3: Android底层开发是指在Android操作系统上进行系统级别的功能开发Android底层开发主要包括四个方面的内容:内核开发、HAL(硬件抽象层)开发、驱动程序开发和底层库开发。 首先,内核开发是指对Android系统内核进行修改和优化,以满足特定需求,并提供更好的性能和稳定性。内核开发需要对操作系统的核心组件进行深入研究和理解,包括进程管理、内存管理、文件系统等。 其次,HAL开发通过编写硬件抽象层的代码,将硬件和操作系统进行连接。这样做的目的是为了让Android系统能够支持不同品牌和型号的硬件设备,如传感器、摄像头、显示屏等。HAL开发需要理解硬件设备的工作原理和规范,并编写对应的接口和驱动程序。 驱动程序开发是为了让Android系统能够正确地识别和使用硬件设备。驱动程序是一种特殊的软件,用于与硬件设备进行通信和控制。驱动程序开发需要具备底层编程语言的知识,如C或C++,以及硬件设备的技术规范和接口协议。 最后,底层库开发是指编写和优化Android系统底层的库文件,以提供一些基础的功能和服务,如网络通信、图形显示、音频处理等。底层库开发需要对操作系统的功能和架构有深入了解,并具备编程技巧和算法优化的能力。 总之,Android底层开发需要深入理解操作系统和硬件设备的工作原理,具备底层编程语言的知识和技能,以及良好的系统分析和优化能力。通过进行Android底层开发开发者能够深入了解Android系统的运行原理,提升系统性能,并实现定制化和优化的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

willwaywang6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值