服务必须使布局膨胀以显示为浮动窗口小部件。我可以安装非实质性的UI组件。但是,当我使用诸如按钮之类的重要组件时,应用会崩溃并显示以下错误。
java.lang.RuntimeException: Unable to create service com.example.WindowService: android.view.InflateException: Binary XML file line #82: Binary XML file line #82: Error inflating class com.google.android.material.button.MaterialButton
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3740)
at android.app.ActivityThread.access$1400(ActivityThread.java:235)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1784)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Caused by: android.view.InflateException: Binary XML file line #82: Binary XML file line #82: Error inflating class com.google.android.material.button.MaterialButton
Caused by: android.view.InflateException: Binary XML file line #82: Error inflating class com.google.android.material.button.MaterialButton
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.view.View.inflate(View.java:26170)
at com.example.WindowService.onCreate(WindowService.kt:29)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3728)
at android.app.ActivityThread.access$1400(ActivityThread.java:235)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1784)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:240)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:215)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:143)
at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:78)
at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:200)
at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:191)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.view.View.inflate(View.java:26170)
at com.example.WindowService.onCreate(WindowService.kt:29)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3728)
at android.app.ActivityThread.access$1400(ActivityThread.java:235)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1784)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
内部服务onCreate。该行抛出以上错误。
floatingView = View.inflate(this, R.layout.floating_view, null)
floating_layout包含具有数据绑定的ConstraintLayout,且位于材料小部件下方。
<com.google.android.material.button.MaterialButton
android:id="@+id/material_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/device_name"/>
我尝试将AppTheme更改为<application>
标签的几个可用的素材主题,但是仍然存在相同的问题。
答案 0 :(得分:0)
根据 @StanislavBondar 的评论,能够在使用中使用材料组件来增加布局。
floatingView = View.inflate(new ContextThemeWrapper(this, R.style.AppTheme), R.layout.floating_view)