kotlin 越来越火,但使用的越多发现不顺手的地方也越多。
最近发现了崩溃:
11-17 00:54:07.355 266-266/? A/DEBUG: signal 6 (SIGABRT), code -6 (SI_TKILL),
fault addr -------- 11-17 00:54:07.359 266-266/? A/DEBUG: Abort message:
'art/runtime/art_method.cc:219] Failed to find Dex offset for PC offset
0xa4e(PC 0xe032e5ba, entry_point=0xe032db6c current entry_point=0xe032db6c) in
java.lang.String com.adikia.Origin.print(java.lang.String)' eax 00000000 ebx
00003578 ecx 00003578 edx 00000006 esi f7795c50 edi 00000000 xcs 00000023 xds
0000002b xes 0000002b xfs 00000007 xss 0000002b eip f73406c6 ebp 00003578 esp
ff8b85f0 flags 00200206 11-17 00:54:07.361 266-266/? A/DEBUG: backtrace: #00 pc
000836c6 /system/lib/libc.so (tgkill+22) #01 pc 00081728 /system/lib/libc.so
(pthread_kill+70) #02 pc 00027255 /system/lib/libc.so (raise+36) #03 pc
00020a34 /system/lib/libc.so (abort+80) #04 pc 0051b2bb /system/lib/libart.so
(art::Runtime::Abort()+377) #05 pc 00151759 /system/lib/libart.so
(art::LogMessage::~LogMessage()+1383) #06 pc 0014bc66 /system/lib/libart.so
(art::Barrier::~Barrier()+966) #07 pc 00564aff /system/lib/libart.so
(art::ThreadList::Dump(std::__1::basic_ostream<char,
std::__1::char_traits<char> >&)+271) #08 pc 0051b438 /system/lib/libart.so
(art::Runtime::Abort()+758) #09 pc 00151759 /system/lib/libart.so
(art::LogMessage::~LogMessage()+1383) #10 pc 001484f6 /system/lib/libart.so
(art::ArtMethod::ToDexPc(unsigned int, bool)+2662) #11 pc 0052b6ca
/system/lib/libart.so (art::StackVisitor::GetDexPc(bool) const+68) #12 pc
00541a01 /system/lib/libart.so
(art::BuildInternalStackTraceVisitor<false>::VisitFrame()+131) #13 pc 0052eef3
/system/lib/libart.so (art::StackVisitor::WalkStack(bool)+243) #14 pc 0055277b
/system/lib/libart.so (_jobject*
art::Thread::CreateInternalStackTrace<false>(art::ScopedObjectAccessAlreadyRunnable
const&) const+329) #15 pc 00483da1 /system/lib/libart.so
(art::Throwable_nativeFillInStackTrace(_JNIEnv*, _jclass*)+52) #16 pc 730496be
/data/dalvik-cache/x86/system@[email protected] (offset 0x1ef8000)
这是什么鬼,完全看不到原因,后来发现这是由书写不规范而与项目本身的一系列连锁反应引起的。
首先我写了一个java的类:
public class CallbackTest { public Object callback(){ return null; } }
然后再kotlin中复写调用:
object :CallbackTest(){ override fun callback(): Any {
Log.v("XPC","origin_method"); return super.callback() } }.callback()
这样就会报下面的错误
11-17 02:36:01.446 18132-18132/com.adikia E/AndroidRuntime: FATAL EXCEPTION:
main Process: com.adikia, PID: 18132 java.lang.IllegalStateException:
super.callback() must not be null at
com.adikia.MainActivity$onClick$2.callback(MainActivity.kt:49) at
com.adikia.MainActivity.onClick(MainActivity.kt:51) at
android.view.View.performClick(View.java:5198) at
android.view.View$PerformClick.run(View.java:21147) at
android.os.Handler.handleCallback(Handler.java:739) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:148) at
android.app.ActivityThread.main(ActivityThread.java:5417) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at
de.robv.android.xposed.XposedBridge.main(XposedBridge.java:107)
其实这个错误很简单,就是返回值不能是null,如果是null我们需要指定:Any?,下面的写法是正确的
object :CallbackTest(){ override fun callback(): Any? { return
super.callback() } }.callback()
但问题就在于自动生成重载方法的时候,并没有自动加?这就导致类经常会忘记添加,而真正的项目中情况复杂,一旦发生连锁反应,将会很难排查。
热门工具 换一换