最近,我添加了“使用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(主屏幕活动)
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
,并尝试在DrawerLayout
代码中将其转换为Activity
。在布局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/