java - 必须使用 MeasureSpec.EXACTLY 测量 Android Studio 和 DrawerLayout 的问题
问题描述
我是一名大学生,仍在学习如何使用 Android Studio。有人可以帮我吗?当我进入新闻登录时,它一直在崩溃,并向我发送此错误。它说它是 DrawerLayout,但我已经尝试了一段时间,对于我的生活,我找不到如何修复它。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.librolaneandroidportal, PID: 4986
java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
at androidx.drawerlayout.widget.DrawerLayout.onMeasure(DrawerLayout.java:1053)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:747)
at android.view.View.measure(View.java:25466)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3397)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2228)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2486)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
at android.view.Choreographer.doCallbacks(Choreographer.java:796)
at android.view.Choreographer.doFrame(Choreographer.java:731)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 4986 SIG: 9
这是Java文件
package com.example.librolaneandroidportal;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//Variables
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Hooks
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
toolbar = findViewById(R.id.toolbar);
//Toolbar
setSupportActionBar(toolbar);
//Navigation Drawer Menu
navigationView.bringToFront();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.nav_home);
}
@Override
public void onBackPressed() {
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
else{
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home:
break;
case R.id.nav_opac:
Intent intent = new Intent(MainActivity.this, Opac.class);
startActivity(intent);
break;
case R.id.nav_qr:
Intent intent2 = new Intent(MainActivity.this, QRscanner.class);
startActivity(intent2);
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
这是我制作的布局。
<?xml version="1.0" encoding="utf-8"?>
<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/drawer_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/nav_view"
app:headerLayout="@layout/header"
app:menu="@menu/main_menu"
android:layout_gravity="start"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_background">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="@drawable/ic_baseline_menu_24" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="175dp"
android:background="@drawable/round_layouts"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.cardview.widget.CardView
android:id="@+id/OPACbutton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:padding="10dp"
app:cardBackgroundColor="@color/ocean_green"
app:cardCornerRadius="15dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/opacbtnimage"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
app:srcCompat="@drawable/opac_btnicon" />
<TextView
android:id="@+id/opacbtnlabel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/opacbtnimage"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:text="@string/OPAC_btn_text"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/QRbutton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:padding="10dp"
app:cardBackgroundColor="@color/ocean_green"
app:cardCornerRadius="15dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/qrbtnimage"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
app:srcCompat="@drawable/scanner_btnicon" />
<TextView
android:id="@+id/qrbtnlabel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/qrbtnimage"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:text="@string/QR_btn_text"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/Reservebutton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:padding="10dp"
app:cardBackgroundColor="@color/ocean_green"
app:cardCornerRadius="15dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/reservebtnimage"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
app:srcCompat="@drawable/opac_btnicon" />
<TextView
android:id="@+id/reservebtnlabel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/reservebtnimage"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:text="@string/Reserve_btn_text"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="175dp"
android:background="@color/white"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent">
<androidx.cardview.widget.CardView
android:id="@+id/Favoritesbutton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:padding="10dp"
app:cardBackgroundColor="@color/ocean_green"
app:cardCornerRadius="15dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/favoritesbtnimage"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
app:srcCompat="@drawable/opac_btnicon" />
<TextView
android:id="@+id/favoritesbtnlabel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/favoritesbtnimage"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:text="@string/Favorite_btn_text"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/Notificationbutton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:padding="10dp"
app:cardBackgroundColor="@color/ocean_green"
app:cardCornerRadius="15dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/notificationbtnimage"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
app:srcCompat="@drawable/opac_btnicon" />
<TextView
android:id="@+id/notificationbtnlabel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/notificationbtnimage"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:text="@string/Notifications_btn_text"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/Feedbackbutton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:padding="10dp"
app:cardBackgroundColor="@color/ocean_green"
app:cardCornerRadius="15dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/Feedbackbtnimage"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
app:srcCompat="@drawable/opac_btnicon" />
<TextView
android:id="@+id/Feedbackbtnlabel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/Feedbackbtnimage"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:text="@string/Feedback_btn_text"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
解决方案
使 DrawerLayout 和 NavigationViews 的高度和宽度为“match_parent”。希望它会工作:)
推荐阅读
- ruby - 安装 Ruby 'rbenv: shell integration not enabled...' - 在 macOS Mojave 10.14.6 上安装自制软件
- android - 离子:通过firebase从设备发送推送通知到设备:从iOS发送不起作用
- javascript - 想检查一个数组是否有另一个数组的值,如果有,则更改第一个数组的值并再次检查
- python - 使用 Pulp 进行线性编程:LpAffineExpression 出错
- flutter - 使用 Flutter 在 Firebase 实时数据库中批量写入(用于非规范化数据)?
- ios - Swift:以编程方式更改具有委托的另一个 ViewController 的值
- c# - 将关联 ID 从代码发送到 SQL Server
- python - 列表中所有元素的长度之和
- r - 重复序列内的cumsum
- php - 将发布的 php 数组转换为多维数组