活动和布局在更改样式android之后导致崩溃

问题描述 投票:0回答:1

在我的项目中实现后,我用sidebar menu构建了一个简单的活动主体,提供的示例效果很好

我最有可能是由样式主题工具栏引起的崩溃,或者我的[[layout损坏了。

作为解决方法,我用于注释setSupportActionBar(toolbar)并更改主题

AndroidManifest主题

android:theme =“ @ style / AppTheme”

使用的Styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style>

MainActivity

DrawerLayout drawerLayout; private ActionBarDrawerToggle drawerListener; Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); drawerLayout = findViewById(R.id.drawerLayout); drawerListener = new ActionBarDrawerToggle(this,drawerLayout,R.string.drawer_open,R.string.drawer_close); drawerLayout.addDrawerListener(drawerListener); drawerListener.syncState(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if(drawerListener.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); }

activity_main

<androidx.drawerlayout.widget.DrawerLayout android:id="@+id/drawerLayout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" > <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent" > <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.NoActionBar"> <androidx.appcompat.widget.Toolbar android:layout_height="?attr/actionBarSize" android:layout_width="match_parent" android:background="@color/colorPrimary" android:id="@+id/toolbar" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> </com.google.android.material.appbar.AppBarLayout> <LinearLayout android:id="@+id/mainContent" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"> <ImageView android:id="@+id/imageScanned" android:layout_width="match_parent" android:layout_height="400dp" app:srcCompat="@drawable/ic_camera_alt_black_24dp" android:contentDescription="@string/desc_scan" /> </LinearLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout> <com.google.android.material.navigation.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" app:menu="@menu/top_navigation_menu" android:layout_gravity="left" app:headerLayout="@layout/header_layout" android:background="@drawable/shape1" android:theme="@style/ThemeOverlay.AppCompat.Dark" /> </androidx.drawerlayout.widget.DrawerLayout>

这是logcat所说的

11-26 21:49:38.610 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到从方法androidx.core.view.ViewCompat.addOnUnhandledKeyEventListener引用的类'androidx.core.view.ViewCompat $ 2'

11-26 21:49:38.610 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到类'android.view.WindowInsets',从方法androidx.core.view.ViewCompat.dispatchApplyWindowInsets引用

11-26 21:49:38.620 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到类'android.view.WindowInsets',从方法androidx.core.view.ViewCompat.onApplyWindowInsets引用

11-26 21:49:38.630 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到从方法androidx.core.view.ViewCompat.removeOnUnhandledKeyEventListener引用的类'android.view.View $ OnUnhandledKeyEventListener' >

11-26 21:49:38.630 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到类androidx.core.view.ViewCompat $ 1,从方法androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener引用

11-26 21:49:38.680 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到类“ android.graphics.drawable.RippleDrawable”,从方法androidx.appcompat.widget.AppCompatImageHelper.hasOverlappingRendering引用>

11-26 21:49:38.750 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到从方法androidx.appcompat.widget.AppCompatTextClassifierHelper.getTextClassifier引用的类'android.view.textclassifier.TextClassificationManager' >

11-26 21:50:10.260 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到类'androidx.drawerlayout.widget.DrawerLayout $ 1',从方法androidx.drawerlayout.widget.DrawerLayout引用。] >

11-26 21:50:10.260 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到类'android.view.WindowInsets',从方法androidx.drawerlayout.widget.DrawerLayout.onDraw中引用

11-26 21:50:10.270 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到类android.view.WindowInsets,从方法androidx.drawerlayout.widget.DrawerLayout.onMeasure引用

11-26 21:50:10.270 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到类android.view.WindowInsets,从方法androidx.drawerlayout.widget.DrawerLayout.onMeasure引用

我使用侧边栏菜单构建了一个简单的活动主体,所提供的示例效果很好,在我的项目中实施后,我最有可能是崩溃的原因是样式主题工具栏或布局被破坏。 ...

通过更改布局固定

activity_main

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.coordinatorlayout.widget.CoordinatorLayout android:fitsSystemWindows="true" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" android:layout_width="match_parent" android:layout_height="match_parent" > <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" > </com.google.android.material.appbar.AppBarLayout> <LinearLayout android:id="@+id/mainContent" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"> <androidx.appcompat.widget.Toolbar android:layout_height="?attr/actionBarSize" android:layout_width="match_parent" android:background="@color/colorPrimary" android:id="@+id/toolbar" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> <ImageView android:id="@+id/imageScanned" android:layout_width="match_parent" android:layout_height="400dp" app:srcCompat="@drawable/ic_camera_alt_black_24dp" android:contentDescription="@string/desc_scan" /> </LinearLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout> <com.google.android.material.navigation.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" app:menu="@menu/top_navigation_menu" android:layout_gravity="left" app:headerLayout="@layout/header_layout" android:background="@drawable/shape1" android:theme="@style/ThemeOverlay.AppCompat.Dark" /> </androidx.drawerlayout.widget.DrawerLayout>

android xml styles toolbar android-theme
1个回答
0
投票
通过更改布局固定

activity_main

© www.soinside.com 2019 - 2024. All rights reserved.