有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓。小装置。RelativeLayout不能投射到安卓x。抽屉布局。小装置。抽屉布局

最近我添加了“用谷歌登录”按钮,效果很好,但现在当我尝试运行应用程序时,这个错误会弹出,我不知道为什么

这里有一个完整的例外:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.miapp, PID: 26644
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.miapp/com.example.loginapp.HomeScreen}: java.lang.ClassCastException: 安卓.widget.RelativeLayout cannot be cast to 安卓x.drawerlayout.widget.DrawerLayout
        at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:3114)
        at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
        at 安卓.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at 安卓.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at 安卓.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
        at 安卓.os.Handler.dispatchMessage(Handler.java:106)
        at 安卓.os.Looper.loop(Looper.java:214)
        at 安卓.app.ActivityThread.main(ActivityThread.java:7050)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:965)
     Caused by: java.lang.ClassCastException: 安卓.widget.RelativeLayout cannot be cast to 安卓x.drawerlayout.widget.DrawerLayout
        at com.example.loginapp.HomeScreen.onCreate(HomeScreen.java:31)
        at 安卓.app.Activity.performCreate(Activity.java:7327)
        at 安卓.app.Activity.performCreate(Activity.java:7318)
        at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094)
        at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257) 
        at 安卓.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at 安卓.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at 安卓.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1948) 
        at 安卓.os.Handler.dispatchMessage(Handler.java:106) 
        at 安卓.os.Looper.loop(Looper.java:214) 
        at 安卓.app.ActivityThread.main(ActivityThread.java:7050) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:965)

错误出现在这一行,似乎是:

        dl = findViewById(R.id.dl);

以下是主要活动(主屏幕活动)

package com.example.loginapp;

import 安卓x.annotation.NonNull;
import 安卓x.appcompat.app.ActionBarDrawerToggle;
import 安卓x.appcompat.app.AppCompatActivity;
import 安卓x.drawerlayout.widget.DrawerLayout;

import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.widget.Toast;

import com.google.安卓.material.navigation.NavigationView;
import com.google.firebase.auth.FirebaseAuth;

public class HomeScreen extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;
    private DrawerLayout dl;
    private ActionBarDrawerToggle abdt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);

        NavigationView nav_view = findViewById(R.id.nav_view);

        dl = findViewById(R.id.dl);
        abdt = new ActionBarDrawerToggle(this, dl,R.string.Open, R.string.Close);
        abdt.setDrawerIndicatorEnabled(true);

        firebaseAuth = FirebaseAuth.getInstance();

        dl.addDrawerListener(abdt);
        abdt.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        nav_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int id = menuItem.getItemId();

                if (id == R.id.miPerfil){

                    startActivity(new Intent(HomeScreen.this, MiPerfil.class));
                }

                if (id == R.id.ayudaPerfil){

                    Toast.makeText(HomeScreen.this, "Ayuda", Toast.LENGTH_SHORT).show();
                }

                if (id == R.id.agregarLista){
                    startActivity(new Intent(HomeScreen.this, HomeActivity.class));
                    Toast.makeText(HomeScreen.this, "Añadir actividad", Toast.LENGTH_SHORT).show();
                }

                if (id == R.id.logoutMenu){
                    Toast.makeText(HomeScreen.this, "Cerrando sesión... ", Toast.LENGTH_SHORT).show();
                    Logout();
                }

                return true;
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.navigation_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item){

        return super.onOptionsItemSelected(item) || abdt.onOptionsItemSelected(item);
    }

    private void Logout(){
        firebaseAuth.signOut();
        finish();
        startActivity(new Intent(HomeScreen.this, MainActivityy.class));
    }
}

以下是XML

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:background="#FFF"
    安卓:orientation="vertical"
    安卓:id="@+id/dl">

    <RelativeLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:background="#000">

    </RelativeLayout>
    <com.google.安卓.material.navigation.NavigationView
        安卓:layout_width="wrap_content"
        安卓:layout_height="match_parent"
        安卓:id="@+id/nav_view"
        安卓:layout_gravity="start"
        安卓:background="#FFF"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/navigation_menu">

    </com.google.安卓.material.navigation.NavigationView>
</RelativeLayout>

拜托,如果你能帮忙,我很感激


共 (1) 个答案

  1. # 1 楼答案

    堆栈跟踪清楚地表明了问题所在。您正在xml中使用RelativeLayout,并尝试在Activity代码中将其转换为DrawerLayout。在布局xml中使用androidx.drawerlayout.widget.DrawerLayout而不是RelativeLayout

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFF"
        android:fitsSystemWindows="true"
        tools:openDrawer="start"
        android:id="@+id/dl">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000">
    
        </RelativeLayout>
        <com.google.android.material.navigation.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/nav_view"
            android:layout_gravity="start"
            android:background="#FFF"
            app:headerLayout="@layout/navigation_header"
            app:menu="@menu/navigation_menu">
    
        </com.google.android.material.navigation.NavigationView>
    </androidx.drawerlayout.widget.DrawerLayout>