E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.debugshow, PID: 3438 java.lang.RuntimeException: Unable to start service com.example.debugshow.view.Main.FloatingService@ed88d6d with Intent { cmp=com.example.debugshow/.view.Main.FloatingService (has extras) }: android.view.InflateException: Binary XML file line #22: Binary XML file line #22: Error inflating class com.example.debugshow.view.Main.DraggableFloatingButton at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3097) at android.app.ActivityThread.access$2300(ActivityThread.java:156) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5653) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) Caused by: android.view.InflateException: Binary XML file line #22: Binary XML file line #22: Error inflating class com.example.debugshow.view.Main.DraggableFloatingButton at android.view.LayoutInflater.inflate(LayoutInflater.java:543) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at android.view.View.inflate(View.java:19830) at com.example.debugshow.view.Main.FloatingDebugView.initView(FloatingDebugView.java:37) at com.example.debugshow.view.Main.FloatingDebugView.<init>(FloatingDebugView.java:31) at com.example.debugshow.view.Main.FloatingDebugWindowManager.addView(FloatingDebugWindowManager.java:52) at com.example.debugshow.view.Main.FloatingService.onStartCommand(FloatingService.java:41) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3077) at android.app.ActivityThread.access$2300(ActivityThread.java:156) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5653) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class com.example.debugshow.view.Main.DraggableFloatingButton at android.view.LayoutInflater.createView(LayoutInflater.java:649) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708) at android.view.LayoutInflater.rInflate(LayoutInflater.java:839) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) at android.view.LayoutInflater.rInflate(LayoutInflater.java:842) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) at android.view.LayoutInflater.inflate(LayoutInflater.java:519) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at android.view.View.inflate(View.java:19830) at com.example.debugshow.view.Main.FloatingDebugView.initView(FloatingDebugView.java:37) at com.example.debugshow.view.Main.FloatingDebugView.<init>(FloatingDebugView.java:31) at com.example.debugshow.view.Main.FloatingDebugWindowManager.addView(FloatingDebugWindowManager.java:52) at com.example.debugshow.view.Main.FloatingService.onStartCommand(FloatingService.java:41) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3077) at android.app.ActivityThread.access$2300(ActivityThread.java:156) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5653) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at android.view.LayoutInflater.createView(LayoutInflater.java:623) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708) at android.view.LayoutInflater.rInflate(LayoutInflater.java:839) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) at android.view.LayoutInflater.rInflate(LayoutInflater.java:842) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) at android.view.LayoutInflater.inflate(LayoutInflater.java:519) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at android.view.View.inflate(View.java:19830) at com.example.debugshow.view.Main.FloatingDebugView.initView(FloatingDebugView.java:37) at com.example.debugshow.view.Main.FloatingDebugView.<init>(FloatingDebugView.java:31) at com.example.debugshow.view.Main.FloatingDebugWindowManager.addView(FloatingDebugWindowManager.java:52) at com.example.debugshow.view.Main.FloatingService.onStartCommand(FloatingService.java:41) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3077) at android.app.ActivityThread.access$2300(ActivityThread.java:156) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5653) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant). at android.support.design.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:221) at android.support.design.internal.ThemeEnforcement.checkAppCompatTheme(ThemeEnforcement.java:196) at android.support.design.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:131) at android.support.design.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:72) at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:195) at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:188) at com.example.debugshow.view.Main.DraggableFloatingButton.<init>(DraggableFloatingButton.java:24) at java.lang.reflect.Constructor.newInstance(Native Method) at android.view.LayoutInflater.createView(LayoutInflater.java:623) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708) at android.view.LayoutInflater.rInflate(LayoutInflater.java:839) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) at android.view.LayoutInflater.rInflate(LayoutInflater.java:842) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) at android.view.LayoutInflater.inflate(LayoutInflater.java:519) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at android.view.View.inflate(View.java:19830) at com.example.debugshow.view.Main.FloatingDebugView.initView(FloatingDebugView.java:37) at com.example.debugshow.view.Main.FloatingDebugView.<init>(FloatingDebugView.java:31) at com.example.debugshow.view.Main.FloatingDebugWindowManager.addView(FloatingDebugWindowManager.java:52) at com.example.debugshow.view.Main.FloatingService.onStartCommand(FloatingService.java:41) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3077) at android.app.ActivityThread.access$2300(ActivityThread.java:156) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5653) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) I/Process: Sending signal. PID: 3438 SIG: 9 Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
網上不少說法是出這個錯的緣由是自定義View的構造函數沒寫全,可是我把三個構造函數寫全了,依然報這個錯。其實真正的解法是,在引用這個自定義佈局的界面根標籤裏,添加一行代碼:java
android:theme="@style/Theme.AppCompat"