Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Studio和抽屉布局出现问题时,必须使用MeasureSpec进行精确测量_Java_Android_Xml_Android Layout_Drawerlayout - Fatal编程技术网

Java Android Studio和抽屉布局出现问题时,必须使用MeasureSpec进行精确测量

Java Android Studio和抽屉布局出现问题时,必须使用MeasureSpec进行精确测量,java,android,xml,android-layout,drawerlayout,Java,Android,Xml,Android Layout,Drawerlayout,我是一名大学生,还在学习如何使用Android Studio。有人能帮我吗?当我登录到press时,它不断崩溃,并向我发送此错误。上面说这是抽屉的布局,但我已经尝试了一段时间了,在我的一生中,我找不到如何修复它 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.librolaneandroidportal, PID: 4986 java.lang.IllegalArgumentException: Drawe

我是一名大学生,还在学习如何使用Android Studio。有人能帮我吗?当我登录到press时,它不断崩溃,并向我发送此错误。上面说这是抽屉的布局,但我已经尝试了一段时间了,在我的一生中,我找不到如何修复它

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>


尝试更改抽屉布局的布局宽度以匹配父项。哦,这很有效。不知道这么简单。非常感谢你!成功了。