Я искал здесь похожие проблемы, но решение для меня не помогло. Я хочу внедрить навигационный ящик в мою основную деятельность, но когда я запускаю его, у меня появляется ошибка с нулевым исключением, но я не могу ее решить. Основная деятельность. java заинтересованная часть выглядит следующим образом:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private static List<Nota> notas;
private static RecyclerView recyclerView;
private static RecyclerViewAdapter adapter;
private static Context baseContext;
private static View viewLayout;
private DatabaseReference myRef;
private static final String TAG= MainActivity.class.getName();
private FirebaseAuth mAuth;
private GoogleSignInClient mGoogleSignInClient;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
drawerLayout = findViewById(R.id.drawerLayout);
navigationView= findViewById(R.id.navigation_drawer);
setSupportActionBar(toolbar);
viewLayout = findViewById(R.id.viewLayout);
baseContext = MainActivity.this;
navigationView.setNavigationItemSelectedListener(this);
toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_open, R.string.navigation_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
notas = new ArrayList<>();
if (savedInstanceState == null){
myRef = FirebaseDatabase.getInstance().getReference();
mAuth = FirebaseAuth.getInstance();
}
recyclerView= findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
adapter= new RecyclerViewAdapter(notas,MainActivity.this);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id)
{
case R.id.inbox_id:
Toast.makeText(this, "Hai selezionato Inbox", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id.delete_id:
Toast.makeText(this, "Hai selezionato Delete", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id.sent_id:
Toast.makeText(this, "Hai selezionato Sent", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id.tutte_le_email_id:
Toast.makeText(this, "Hai selezionato Tutte le email", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
}
return false;
}
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
notas.clear();
for (DataSnapshot obj : dataSnapshot.getChildren()){
String key = obj.getKey();
Nota nota = dataSnapshot.child(key).getValue(Nota.class);
if (nota != null) notas.add(nota);
}
if (notas.size()>0) {
recyclerView = viewLayout.findViewById(R.id.recyclerView);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(baseContext);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new RecyclerViewAdapter(notas, baseContext);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
Activity_main. xml is:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/viewLayout"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:backgroundTint="@color/pulsanti"
app:srcCompat="@drawable/ic_addnote"
app:maxImageSize="38dp"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
И навигационный_текстер. 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
tools:openDrawer="start">
<include layout="@layout/activity_main"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="start"
app:headerLayout="@layout/header_nav_drawer"
app:menu="@menu/menu_navigation_drawer"/>
</androidx.drawerlayout.widget.DrawerLayout>
Без панели навигации все работает нормально, но с кодом выше я получил эту ошибку:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.applicazionenote, PID: 18736
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.applicazionenote/com.example.applicazionenote.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.navigation.NavigationView.setNavigationItemSelectedListener(com.google.android.material.navigation.NavigationView$OnNavigationItemSelectedListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3488)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3635)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2175)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7857)
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:1076)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.navigation.NavigationView.setNavigationItemSelectedListener(com.google.android.material.navigation.NavigationView$OnNavigationItemSelectedListener)' on a null object reference
at com.example.applicazionenote.MainActivity.onCreate(MainActivity.java:85)
at android.app.Activity.performCreate(Activity.java:7955)
at android.app.Activity.performCreate(Activity.java:7944)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3463)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3635)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2175)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7857)
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:1076)
I / Process: Отправка сигнала. PID: 18736 SIG: 9