android - 以编程方式设置 AppBarLayout 导致 java.lang.IllegalArgumentException 需要 Theme.AppCompat (或后代)
问题描述
AppBarLayout 导致:“java.lang.IllegalArgumentException:此组件上的样式要求您的应用主题为 Theme.AppCompat(或后代)。” 我知道有很多类似的问题有很多答案,但似乎没有一个对我有用。我已经尝试了一切:扩展 AppCompatActivity,将主题设置为 Theme.AppCompat,清理项目,重置缓存,重新启动 Android 工作室,到处乱跑……我绝望了。因此,如果您不知道答案,请不要仅仅为了获得积分而回复。minSdk - 23,目标 - 28
依赖项
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
implementation 'androidx.transition:transition:1.2.0'
implementation 'androidx.percentlayout:percentlayout:1.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation group: 'com.fasterxml', name: 'jackson-module-json-org', version: '0.9.1'
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Descendant">
<activity android:name="com.example.no_xml.MainActivity" android:theme="@style/Descendant">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
样式.xml
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/stgWhite</item>
</style>
<style name="Descendant" parent="AppBaseTheme">
</style>
主要活动
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUpMainContainer();
}
private void setUpMainContainer(){
getWindow().setStatusBarColor(getResources().getColor(R.color.stgTransparent, null));
stgConstraintLayout mainLayout = new stgConstraintLayout(getApplicationContext());
mainLayout.setFitsSystemWindows(true);
setContentView(mainLayout);
CoordinatorLayout mainCoordinator = new stgCoordinatorLayout(getApplicationContext());
FrameLayout bottomNaviContainer = new stgFrameLayout(getApplicationContext());
mainLayout.addView(mainCoordinator, 0);
mainLayout.addView(bottomNaviContainer, 1);
ConstraintSet set = new ConstraintSet();
set.clone(mainLayout);
set.connect(bottomNaviContainer.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
set.connect(bottomNaviContainer.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
set.connect(bottomNaviContainer.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
set.connect(mainCoordinator.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
set.connect(mainCoordinator.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
set.connect(mainCoordinator.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
set.connect(mainCoordinator.getId(), ConstraintSet.BOTTOM, bottomNaviContainer.getId(), ConstraintSet.TOP);
set.applyTo(mainLayout);
}}
调用 AppBarLayout
public class stgCoordinatorLayout extends CoordinatorLayout {
public stgCoordinatorLayout(@NonNull Context context) {
super(context);
setupLayout(context);
}
public stgCoordinatorLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setupLayout(context);
}
public stgCoordinatorLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setupLayout(context);
}
private void setupLayout(Context context){
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
0
);
params.setBehavior(new AppBarLayout.ScrollingViewBehavior());
setLayoutParams(params);
setBackgroundColor(context.getResources().getColor(R.color.stgWhite, null));
setId(View.generateViewId());
ViewPager viewPager = new stgViewPager(context);
addView(viewPager, 0);
viewPager.requestLayout();
AppBarLayout appBarLayout = new stgAppBar(context);
addView(appBarLayout, 1);
}}
AppBarLayout 本身
public class stgAppBar extends AppBarLayout {
public stgAppBar(Context context) {
super(context);
setupLayout(context);
}
public stgAppBar(Context context, AttributeSet attrs) {
super(context, attrs);
setupLayout(context);
}
private void setupLayout(Context context){
AppBarLayout.LayoutParams params = new AppBarLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
setDP(context, 150)
);
setLayoutParams(params);
setElevation(0);
setFitsSystemWindows(true);
setBackgroundColor(context.getResources().getColor(R.color.stgWhite, null));
setId(View.generateViewId());
}}
解决方案
您正在使用getApplicationContext()
,它没有 AppCompat 主题(或任何主题)。this
改为使用您的 Activity 作为您的 Context 对象。
stgConstraintLayout mainLayout = new stgConstraintLayout(this);
推荐阅读
- angular - Bazel 构建 - 服务器执行失败
- c# - 部署更新 api 时如何解决错误请求的 400 错误
- sql - 在oracle sql中向现有json添加新的键值对
- android - 安装新应用时资产文件夹未升级
- kubernetes - kube-apiserver pod 停留在 CreateContainerError 状态
- python - 如何更正python中通过str.contains生成的输出
- angular - Angular Material cdk-virtual-scroll-viewport:如何每行渲染多个项目?
- java - Angular Service Worker 离线支持在部署在 tomcat 上时不起作用
- python - 如何在请求中传递 oauth 令牌而不指定 curl 中传递的客户端 ID?
- vb.net - 如何使用短文本作为数据类型来修复更新代码