美文网首页
Android日常错误记录

Android日常错误记录

作者: Jumping_张明 | 来源:发表于2018-12-26 12:35 被阅读0次

    环信即时通讯

    Q:找不到AsyncTaskCompatl类 NoClassDefFoundError: support/v4/os/AsyncTaskCompat
    A:因为AsyncTaskCompat此类已在API26.0.0以上中被弃用了,所以需要更改compileSdkVersion最高26,同理targetSdkVersion和其它依赖也需要适当降低版本。

    Service里启动Activity延时问题

    Q:Service里监听按键然后启动Activity,大约延时2-3s左右才启动
    A:使用延时意图

        intent intent = new Intent(context, AnotherActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        try {
            pendingIntent.send(); 
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    

    getSupportFragmentManager()报红

    app包下FragmentManager用:
    Fragmentmanager fragmentManager=getFragmentManager();
    v-4包的FragmentManager用:
    FragmentManager fragmentManager=getSupportFragmentManager() 获取
    但是getSupportFragmentManager() 有其运用范围,只能在部分activity中运用。当遇到getSupportFragmentManager() 没定义的问题时,修改下activity为FragmentActivity或者AppCompatActivity。

    旋转动画的时候会有停顿

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <rotate
            android:duration="2000"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="-1"
            android:fromDegrees="0"
            android:toDegrees="359"  />
    </set>
    
            Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
            //解决旋转有停顿
            LinearInterpolator lir = new LinearInterpolator();
            rotateAnimation.setInterpolator(lir);
            mIvLoading.startAnimation(rotateAnimation);
    

    Error: Program type already present: android.support.v4.app.INotificationSideChannel

    当项目使用API28对应的是android9.0,当AndroidStudio运行项目的时候出现编译错误:Error: Program type already present: android.support.v4.app.INotificationSideChannel,这意思是v4包冲突了。
    首先,查找一下自己的文件里是否存在v4包重复的,搜索之后发现没有出现重复,针对这个问题查看了网上一些资料,发现这个问题是AndroidX版本引起的。有以下两种解决方法:

    1、Refactor-->Migrate to AndroidX

    2、向gradle.properties添加以下内容:
    android.enableJetifier=true
    android.useAndroidX=true

    Android与AndroidX包重复问题详情可查看这篇文章:https://www.jianshu.com/p/1466ebefe4d0
    如果SDK不是28的版本,可在Terminal窗口复制命令gradlew clean build --stacktrace去查看详细信息针对性去解决问题~

    zip4j jar包使用中遇到解压文件名字部分出现乱码

    ZipFile zipFile = new ZipFile(zipfilenow.getAbsolutePath());
    zipFile.setFileNameCharset(getCharset(zipfilenow.getPath()));
    
    /**
         * 判断文件的编码格式
         *
         * @param fileName
         * @return
         */
        public static String getCharset(String fileName) {
            BufferedInputStream bis = null;
            String charset = "GBK";
            byte[] first3Bytes = new byte[3];
            try {
                boolean checked = false;
                bis = new BufferedInputStream(new FileInputStream(fileName));
                bis.mark(0);
                int read = bis.read(first3Bytes, 0, 3);
                if (read == -1)
                    return charset;
                if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
                    charset = "UTF-16LE";
                    checked = true;
                } else if (first3Bytes[0] == (byte) 0xFE
                        && first3Bytes[1] == (byte) 0xFF) {
                    charset = "UTF-16BE";
                    checked = true;
                } else if (first3Bytes[0] == (byte) 0xEF
                        && first3Bytes[1] == (byte) 0xBB
                        && first3Bytes[2] == (byte) 0xBF) {
                    charset = "UTF-8";
                    checked = true;
                }
                bis.mark(0);
                if (!checked) {
                    while ((read = bis.read()) != -1) {
                        if (read >= 0xF0)
                            break;
                        if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
                            break;
                        if (0xC0 <= read && read <= 0xDF) {
                            read = bis.read();
                            if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
                                // (0x80 - 0xBF),也可能在GB编码内
                                continue;
                            else
                                break;
                        } else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
                            read = bis.read();
                            if (0x80 <= read && read <= 0xBF) {
                                read = bis.read();
                                if (0x80 <= read && read <= 0xBF) {
                                    charset = "UTF-8";
                                    break;
                                } else
                                    break;
                            } else
                                break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
            return charset;
        }
    

    相关文章

      网友评论

          本文标题:Android日常错误记录

          本文链接:https://www.haomeiwen.com/subject/jmexqqtx.html