Android 异常点滴汇总

本文汇总了Android开发中常见的异常情况及其解决方案,包括由Fragment包导入错误引起的动画异常、配置更改不生效的问题、隐式启动Service的安全警告以及蓝牙连接失败等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 异常点滴汇总

  1.Caused by: Java.lang.RuntimeException: Unknown animation name: objectAnimator,或者 runtimeexception: Unknow animator name: translate
异常原因:因为Fragment导错包导致;
调用 ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); 加载动画,会触发FragmentManager类中的loadAnimation方法,而该方法加载的动画标签类型在不同的包中支持的标签不一样。可以看下源码。android.app.Fragment中支持的动画标签为:objectAnimator 和 animator 和 set 而 android.support.v4.app.Fragment中支持的动画标签为 set,alpha,scale,translate,rotate.所以如果你的Fragment是应该使用对应支持的动画。

  2.在调用onConfigurationchanged();不执行:
原因:配置不完全。如果设置了android:targetSdkVersion ,记得加上screenSize。
完整配置如下: android:configChanges="orientation|keyboard|keyboardHidden|screenSize|layoutDirection"

 3.隐式启动Service时warming:Implicit intents with startService are not safe: XXX
原因:

Android5.0后,其中有个特性就是Service Intent must be explitict,也就是说从Lollipop开始,service服务必须采用显示方式启动。源码是这样写的(源码位置:sdk/sources/android-21/android/app/ContextImpl.Java解决方法

1.设置Action和packageName:(此方式是google官方推荐使用的解决方法。参考:http://developer.android.com/goo ... tml#billing-service

  1. Intent mIntent = new Intent();  
  2. mIntent.setAction("XXX.XXX.XXX");//你定义的service的action  
  3. mIntent.setPackage(getPackageName());//这里你需要设置你应用的包名  
  4. context.startService(mIntent); 


 2、将隐式启动转换为显示启动:

  1. public static Intent getExplicitIntent(Context context, Intent implicitIntent) {  
  2.  // Retrieve all services that can match the given intent  
  3.  PackageManager pm = context.getPackageManager();  
  4.  List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);  
  5.  // Make sure only one match was found  
  6.  if (resolveInfo == null || resolveInfo.size() != 1) {  
  7.     return null;  
  8.   }  
  9.  // Get component info and create ComponentName  
  10.  ResolveInfo serviceInfo = resolveInfo.get(0);  
  11.  String packageName = serviceInfo.serviceInfo.packageName;  
  12.  String className = serviceInfo.serviceInfo.name;  
  13.  ComponentName component = new ComponentName(packageName, className);  
  14.  // Create a new intent. Use the old one for extras and such reuse  
  15.  Intent explicitIntent = new Intent(implicitIntent);  
  16.  // Set the component to be explicit  
  17.  explicitIntent.setComponent(component);  
  18.  return explicitIntent;  
  19. }
  20. 调用方式如下:
  1. Intent mIntent = new Intent();  
  2. mIntent.setAction("XXX.XXX.XXX");  
  3. Intent eintent = new Intent(getExplicitIntent(mContext,mIntent));  
  4. context.startService(eintent); 

  4.在蓝牙开发过程中:

BluetoothSocket.connect() throws IOException:read failed, socket might closed or timeout, read ret: -1

解决方法:1.在连接之前,需要 取消搜索;2.服务端必须开启【开放检测】功能后再去监听。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值