[英]Activity and layout causing crash after changing style android
在我的項目中實現后,我使用側邊欄菜單構建了一個簡單的活動主界面 ,所提供的示例效果很好
我最有可能是由樣式主題工具欄引起的崩潰,或者我的布局已損壞。
解決方法是,我使用注釋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>
主要活動
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 $ 2'',從方法androidx.core.view.ViewCompat.addOnUnhandledKeyEventListener引用
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:找不到類'android.view.View $ OnUnhandledKeyEventListener',從方法androidx.core.view.ViewCompat.removeOnUnhandledKeyEventListener引用
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:找不到從方法androidx.appcompat.widget.AppCompatImageHelper.hasOverlappingRendering引用的類'android.graphics.drawable.RippleDrawable'
11-26 21:49:38.750 25538-25538 / com.nabeeltech.capturedoc E / dalvikvm:找不到類android.view.textclassifier.TextClassificationManager,從方法androidx.appcompat.widget.AppCompatTextClassifierHelper.getTextClassifier引用
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.