我一直在尝试在我的项目中实现动态它们,但由于某种原因它给出了以下错误。这就是我尝试实现它的方式。
首先我定义了一些自定义属性,例如:
<resources>
<attr name="themeWindowBackground" format="reference" />
<attr name="themeContentBackground" format="reference" />
<attr name="themeContentTextColor" format="reference" />
<attr name="themeContentTextColorOnPrimary" format="reference" />
<attr name="themeTextColorSuccess" format="reference" />
<attr name="themeTextColorFailure" format="reference" />
然后我在 style.xml 文件中定义了自定义主题
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/bg_splash_screen</item>
</style>
<style name="AppTheme.Black" parent="AppTheme">
<item name="colorPrimary">@color/material_dark</item>
<item name="colorPrimaryDark">@color/material_primary_dark</item>
<item name="colorAccent">@color/material_accent</item>
<item name="themeWindowBackground">@color/dark_gray</item>
<item name="themeContentBackground">@color/dark_slate_gray</item>
<item name="themeContentTextColor">@color/white</item>
<item name="themeContentTextColorOnPrimary">@color/teal</item>
<item name="themeTextColorSuccess">@color/green</item>
<item name="themeTextColorFailure">@color/red</item>
</style>
</resources>
之后我用这个设置主题
application.setTheme(mPreferenceRepository.savedTheme)
这是获取已保存主题的函数
public int getSavedTheme() {
String theme = getStringFromSharedPreference(APP_THEME);
if (theme.equals(THEME_BLUE)) {
return R.style.AppTheme_Blue;
}
return R.style.AppTheme_Black;
}
现在当我尝试使用这个在我的布局或布局内容中应用主题时
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="?attr/themeWindowBackground"
android:layout_width="match_parent"
android:layout_height="match_parent">
应用程序崩溃。这是我的信息,但我不知道我做错了什么
Process: com.singularitybd.shellitracker, PID: 2214
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.singularitybd.shellitracker/com.singularitybd.shellitracker.view.activity.LoginActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.singularitybd.shellitracker.view.activity.LoginActivity.onCreate(LoginActivity.kt:44)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f0301ba a=-1}
at android.content.res.TypedArray.getDrawable(TypedArray.java:925)
at android.view.View.<init>(View.java:4202)
at android.view.ViewGroup.<init>(ViewGroup.java:579)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:244)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:240)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.singularitybd.shellitracker.view.activity.LoginActivity.onCreate(LoginActivity.kt:44)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Application terminated.
如果您能给我一些指导,我将非常感激。非常感谢。
当我在旧的 Android 手机上测试我的应用程序时,我刚刚遇到了这个问题。不过它可以在新的 Android 手机上运行。我终于发现我将布局背景图像保存在 res\drawable-v24 中。删除图像并保存回 res\drawable
希望这能解决您的问题
我认为并且据我了解您的陈述,这个问题与RelativeLayout的第二行有关。尝试添加另一张图片到背景,或尝试输入
android:background="?attr/themeWindowBackground"
` 作为风格。
像这样:
style =?attr/themeWindowBackround
这是一个老问题,但我还是决定回答一下。问题是您正在引用一个不存在的视图。
来自您的日志:
Binary XML file line #2: Error inflating class android.widget.RelativeLayout
您正在尝试夸大一个不存在的
RelativeLayout
。
当您添加横向布局而忘记添加
RelativeLayout
时,可能会出现这种情况,或者可能是您引用了 id 不正确的 RelativeLayout
。
确保
RelativeLayout
确实存在,并且在引用它时使用了正确的 id。
当我像这样坐在后台时遇到此错误:
android:background="?attr/itemBackground"
其中:“itemBackground”未在主主题中定义...