错误java.lang.RuntimeException:无法启动活动ComponentInfo{com.tourism.myapplication/com.tourism.myapplication.navigationdrawer}:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void androidx.drawerlayout.widget.DrawerLayout.addDrawerListener(androidx.drawerlayout.widget.DrawerLayout$DrawerListener)”
这是activity_navigationdrawer.xml
<?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:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:id="@+id/drawerLayout"
tools:context=".MainActivity">
<include layout="@layout/app_bar_name"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:id="@+id/navigationView"
app:headerLayout="@layout/header_layout"
app:menu="@menu/menu_item"/>
</androidx.drawerlayout.widget.DrawerLayout>
这是activity_navigationdrawer.xml
<?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:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:id="@+id/drawerLayout"
tools:context=".MainActivity">
<include layout="@layout/app_bar_name"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:id="@+id/navigationView"
app:headerLayout="@layout/header_layout"
app:menu="@menu/menu_item"/>
</androidx.drawerlayout.widget.DrawerLayout>
这是app_bar_name.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ttoolbar"/>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main"/>
</LinearLayout>
这是活动文件
package com.tourism.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class navigationdrawer`your text` extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawerLayout);
navigationView = findViewById(R.id.navigationView);
toolbar = findViewById(R.id.ttoolbar);
//step1
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.OpenDrawer, R.string.CloseDrawer);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
// /**/ loadFragment(new homefrag());
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id==R.id.optTrip){
Toast.makeText(navigationdrawer.this, "", Toast.LENGTH_SHORT).show();
} else if (id==R.id.optHome) {
loadFragment(new homefrag());
} else if (id==R.id.optAbout) {
Toast.makeText(navigationdrawer.this, "", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(navigationdrawer.this, "", Toast.LENGTH_SHORT).show();
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
} else{
super.onBackPressed();
}
}
private void loadFragment(Fragment fragment) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
ft.commit();
}
}
我尝试了所有的解决方案,请帮助
1条答案
按热度按时间anauzrmj1#
根据您的代码,它看起来像一些布局文件使用的问题。在“活动”中,您使用了
R.layout.activity_main
,此处粘贴的代码为activity_navigationdrawer.xml
,因此请将onCreate
方法中的R.layout.activity_main
替换为R.layout.activity_navigationdrawer