gpt4 book ai didi

java - android.widget.RelativeLayout 无法转换为 androidx.drawerlayout.widget.DrawerLayout

转载 作者:行者123 更新时间:2023-11-30 04:54:14 32 4
gpt4 key购买 nike

最近我添加了“使用 google 登录”按钮并且工作正常,但是现在当我尝试运行该应用程序时弹出此错误,我不知道为什么。

这是完整的异常:

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: android.widget.RelativeLayout cannot be cast to androidx.drawerlayout.widget.DrawerLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3114)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to androidx.drawerlayout.widget.DrawerLayout
at com.example.loginapp.HomeScreen.onCreate(HomeScreen.java:31)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7050) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

错误似乎在这一行:

        dl = findViewById(R.id.dl);

这是 MainActivity(主屏幕 Activity )

package com.example.loginapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

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

import com.google.android.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.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:orientation="vertical"
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>
</RelativeLayout>

拜托,如果你能帮上忙,我将不胜感激。

最佳答案

堆栈跟踪清楚地指出了问题。您正在 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>

关于java - android.widget.RelativeLayout 无法转换为 androidx.drawerlayout.widget.DrawerLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59504021/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com