提问人:Reqedd 提问时间:11/23/2022 最后编辑:Reqedd 更新时间:11/23/2022 访问量:72
在 Android Studio 中使用 DrawerLayout 时出现错误 NullPointerException
Getting error NullPointerException when using DrawerLayout in Android Studio
问:
尝试运行我的应用程序时出现 NullPointerException 错误,我将在下面链接代码和 Logcat 错误,非常感谢帮助。
在带有 DrawerLayout 的 XML 类中,我将抽屉的 ID 设置为 drawer_layout(使用 android:id 重命名)。
Logcat 错误
2022-11-23 01:35:49.526 4976-4976/com.example.appdev2finalproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.appdev2finalproject, PID: 4976
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appdev2finalproject/com.example.appdev2finalproject.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.drawerlayout.widget.DrawerLayout.addDrawerListener(androidx.drawerlayout.widget.DrawerLayout$DrawerListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.drawerlayout.widget.DrawerLayout.addDrawerListener(androidx.drawerlayout.widget.DrawerLayout$DrawerListener)' on a null object reference
at com.example.appdev2finalproject.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
来自 Main 的代码
package com.example.appdev2finalproject;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open,
R.string.nav_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(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:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/navigation_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
答:
1赞
Bhavnik
11/23/2022
#1
我看不到资源文件的任何导入语句(即R.layout.file_name)。您似乎从错误的目录导入布局文件。它应该来自 。把这个import语句放进去,你的代码应该可以正常工作了。com.example.appdev2finalproject.R
评论
0赞
Reqedd
11/23/2022
除非我以某种方式误解了你的意思,否则似乎不起作用。
1赞
Reqedd
11/23/2022
#2
发现错误;我忘记在创建新文件后更改 to 以保存抽屉布局layout.activity_main
activity_dashboard
评论
activity_main