1) 总结FC
1.NullPointException 空指针异常
对象.setxxx(),看点之前的对象是否为空。
对象.setxxx(对象B.gettext()),这样就判断两次。
2.ClasscastException 类转换异常
常见:拖动了布局
解决:project--->clean
3.ClassNotFound 指定的类不存在
ActivityNotFound
去检查清单文件。类的声明是否一致,过滤条件是否匹配
4.ArrayIndexOfBoundException 下标越界
5.IllegalArgumentException 非法参数异常。
IllegalStatementException 非法状态异常。
2) 线程&进程
进程:一个应用程序就是一个进程
线程:程序内部的顺序控制流
多进程:一个操作系统中同时运行多个程序。
进程优先级
Foreground process
It hosts an Activity that the user is interacting with (the Activity's onResume() method has been called).
· It hosts a Service that's bound to the activity that the user is interacting with.
· It hosts a Service that's running "in the foreground"—the service has calledstartForeground().
· It hosts a Service that's executing one of its lifecycle callbacks (onCreate(), onStart(), oronDestroy()).
· It hosts a BroadcastReceiver that's executing its onReceive() method.
Visible process
· It hosts an Activity that is not in the foreground, but is still visible to the user (itsonPause() method has been called). This might occur, for example, if the foreground activity started a dialog, which allows the previous activity to be seen behind it.
· It hosts a Service that's bound to a visible (or foreground) activity.
Service process
A process that is running a service that has been started with the startService() method and does not fall into either of the two higher categories.
Background process
A process holding an activity that's not currently visible to the user (the activity's onStop()method has been called).
Empty process
A process that doesn't hold any active application components.
进程间通讯技术 AIDL:
Android Intenrface Definition Language
使用场景:
A 必须含有activity,B必须含有service,
进程间通讯是基于activity跟service的绑定去实现的。
实现步骤:
在服务端创建一个interface(IMyBinder),然后接口中实现方法的声明,方法是service里面的那些方法
把声明的接口的中所有修饰符public全部去掉
找到接口文件,把该文件的后缀改为(.aidl)
在我们services中,声明的那个Mybinder类去继承Imybinder.Stub(联想不到,纯手写)
把aidl文件,全部拷贝到A应用中
在serviceConnect方法中,得到IMyBinder类型的对象。
IMybinder.Stub.asInterface(service)
ANR 对话框
Activity or Application Is Not Responding(ANI)
Main 线程机制:
当主线程执行耗时操作,超过5秒没有响应下一个点击事件,就可能会出现ANR
Main线程的作用:
UI的创建、更新以及事件的处理都是在主线程。
3) Handler机制
步骤:
创建Handler对象
实现handler里面的handleMessage方法
在子线程中需要更新控件的时候,把消息发送过去。
4) ANR产生的原因
在主线程执行耗时超过5秒没有响应下一个事件,就可能出现ANR,(Activity 与 Service)
在BroadcastRevicer中 onReciver方法中,执行耗时操作超过10秒,就会出现ANR
RunOnUiThread()
所有非空控件的以及handle对象的post方法或PostDelayed