• Android开发中常见错误


    1.E/Trace(28187): error opening trace file: No such file or directory (2)

    错误原因:android api版本和模拟器版本不一致,对开发无影响。

    2.android.content.res.Resources$NotFoundException: String resource ID #0x36

    错误原因:一般发生在参数int resId错误,将String赋值给int的resId,所以编译器找不到正确的resource于是报错。检查一下Toast.makeText()、textView.setText之类的函数,这种函数通常有几个重载,如:

    textView.setText(CharSequence text);

    textView.setText(int resId);

    ......

    如果不小心将一个int值传给了它,那它不会显示该int值,而是到工程下去找一个对应的resource的id,当然是找不到的,于是报错。

    解决方案:例:count.setText(incall.getCount());修改为:count.setText(String.valueOf(incall.getCount())); 或者count.setText(incall.getCount() + "");

    3.导入开源项目后,运行应用报错:Caused by: java.lang.ClassNotFoundException: Didn't find class "xxx.xxx.xxx.Xxx" on path: /data/app/xxx.xxx.app-1.apk

    错误原因:eclipse adt 插件升级后导致。

    解决方案:菜单Project -> Properties -> Java Build Path -> Order & Export, 然后选中Android Private Libraries, 菜单 Project->Clean,然后运行程序即可。

    4.报错:Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d0c5c0 that was originally bound here

    错误原因:启动service后,没有将service给unbound掉。android模拟器上自带邮件应用问题。

    解决方案:需要对模拟器进行一些设置,禁用此类无关的应用,具体操作为:"设置"---->"所有应用"---->"Exchange Services"--->"disable"

    5.Android中引入项目报:"invalid resource directory name bin/res/crunch"错误

    错误原因:版本不一致造成的,这个无效的目录不是当前编译的时候生成的,有可能是Debug情况下生成的。因为1.6版本以前只有drawable一个图片资源目录。

    解决方案:直接在项目中删除报错的crunch文件夹。

    6.运行时编译不通过,在Console中报"Unable to execute dex: Multiple dex files define Lcom/nostra13/universalimageloader/cache/disc/DiscCacheAware"错误

    错误原因:Jar包和引用工程中的资源文件重复。

    解决方案:删除重复的jar包。

    7.非UI线程中直接操作UI线程时抛出"android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views"错误

    错误原因:非UI线程中直接操作UI线程。

    错误分析:当每个应用程序apk第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程负责处理与UI相关的事件,如:用户的按键事件,用户接触屏幕的事件以及屏幕绘图事件,并把相关的事件分发到对应的组件进行处理,所以主线程通常又被叫做UI线程。开发Android应用时必须遵守单线程模型的原则:Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行,如果在非UI线程中直接操作UI线程,会抛出android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views,这与普通的java程序不同。由于UI线程负责事件的监听和绘图,因此,必须保证UI线程能够随时响应用户的需求,UI线程里的操作应该向中断事件那样短小,费时的操作(如网络连接)需要另开线程,否则,如果UI线程超过5s没有响应用户请求,会弹出对话框提醒用户终止应用程序(ANR)如果在新开的线程中需要对UI进行设定,就可能违反单线程模型,因此android采用一种的Message Queue机制保证线程间通信

    解决方案:采用消息机制。

    8.导入开源项目,错误提示:Unable to resolve target 'android-16'。R文件无法生成,Clean、Fix Project都不行。

    错误原因:project.properties文件的target版本不存在。

    错误分析:project.properties中的target是指在编译的时候使用哪个版本的API进行编译,和Properties-》Android-》Target name保持一致,无论修改哪一个另外一个值相应改变;minSdkVersion和maxSdkVersion是在程序安装的时候用于指定哪些版本的设备可以安装此应用,一般不需要指定maxSdkVersion;targetSdkVersion是在程序运行的时候起作用,用于提高指定版本的设备上程序运行体验。

    解决方案:关闭项目后重新打开或者Clean一下项目。

    9.新版SDK报错Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

    解决方案:

    方法一:让Activity去直接继承Activity而不是ActionBarActivity,改完之后删掉报错的部分,最后导入Activity的jar包。

    方法二:在AndroidMenifest.xml中将修改theme:android:theme="@style/Theme.AppCompat.Light.NoActionBar",然后在styles.xml中加入主题资源:

         <style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat.Light">
            <item name="android:windowNoTitle">true</item>
         </style>

    10.导入的工程目录中不能有中文,否则会报ERROR: resource directory '/Users/xxx/Desktop/代码/12/rss_reader/res' does not exist。

    11.activity之间传递Bitmap时报错"E/JavaBinder(11688): !!! FAILED BINDER TRANSACTION !!!"

    错误原因:使用Intent传递数据时长度不能大于40k

    解决方案:

    方案一:使用Intent传递Bitmap的地址值

    方案二:使用二进制数组传递

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] bitmapByte = baos.toByteArray();
        bundle.putByteArray("bitmap", bitmapByte);
        intent.putExtras(bundle);

    12.写一个类继承Fragment时,如果有重载构造函数时,会提示"Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead"的错误。

    解决方案:在类的前面加上@SuppressLint("ValidFragment")。

    13.关联库文件失败。

    在windows系统 下,library project必须和project处于相同的盘符中,因为如果在不同盘符,project.properties中的 android.library.reference.1值变成绝对路径,而ADT推荐是在ubuntu下使用的,对windows绝对路径有支持 bug。

  • 相关阅读:
    vsCode 使用 PHP Intelephense插件函数跳转跟踪
    acme.sh 生成证书一直卡在Getting domain auth token for each domain
    用命令行执行php脚本输出乱码
    js检测是否是手机平台
    个人知识结构索引
    离线环境下安装ansible,借助有网环境下pip工具
    zabbix之微信告警(python版):微信个人报警,微信企业号告警脚本
    使用ansible结合FTP部署zabbix_agent
    datax的可视化-datax-web
    Finereport决策报表
  • 原文地址:https://www.cnblogs.com/bianmajiang/p/3951876.html
Copyright © 2020-2023  润新知