理解函数调用栈,可以更好的阅读源码实现逻辑,而且在应用调试过程中追查bug具有积极作用。这里是基于阅读系统源码来讲的。
通常我是运用抛异常,然后看报错日志来追踪函数调用栈的,以Launcher3源码中拖拽开始函数为例:
public void onDragStart(final DragSource source, Object info, int dragAction) {
if(true){
throw new RuntimeException("just test");
}
mIsDragOccuring = true;
updateChildrenLayersEnabled(false);
mLauncher.lockScreenOrientation();
mLauncher.onInteractionBegin();
setChildrenBackgroundAlphaMultipliers(1f);
// Prevent any Un/InstallShortcutReceivers from updating the db while we are dragging
InstallShortcutReceiver.enableInstallQueue();
UninstallShortcutReceiver.enableUninstallQueue();
post(new Runnable() {
@Override
public void run() {
if (mIsDragOccuring) {
addExtraEmptyScreenOnDrag();
}
}
});
}
编译之后的apk重新导入运行,当拖拽app的时候就会报以下错误:
由此可知到拖拽开始的函数调用栈为:
Launcher.onLongClick() -> Workspace.startDrag -> Workspace.beginDragShared() ->DragController.startDrag() -> Workspace.onDragStart()
这里不仅仅知道了函数的调用栈,还可以确定具体报错的位置。