java.lang.RuntimeException: 在空对象引用上无法启动活动 ComponentInfo…

huangapple go评论70阅读模式
英文:

java.lang.RuntimeException: Unable to start activity ComponentInfo... on a null object reference

问题

以下是您要翻译的代码部分:

// 这是活动文件
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 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);

        // 步骤1
        setSupportActionBar(toolbar);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, toolbar, R.string.OpenDrawer, R.string.CloseDrawer);

        drawerLayout.addDrawerListener(toggle);

        toggle.syncState();

        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(Fagment fragment) {

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        ft.add(R.id.container, fragment);
        ft.commit();
    }
}

这是您的Java代码。如果需要翻译XML布局文件的部分,请告诉我。

英文:

The error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tourism.myapplication/com.tourism.myapplication.navigationdrawer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.drawerlayout.widget.DrawerLayout.addDrawerListener(androidx.drawerlayout.widget.DrawerLayout$DrawerListener)' on a null object reference

this is 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>

This is 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>

This is 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>

this is the activity file

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();
}
}

I tried all solutions please help

答案1

得分: 2

根据您的代码,似乎与布局文件使用有些问题。在Activity中,您使用了R.layout.activity_main,而您在此处粘贴的代码是activity_navigationdrawer.xml,所以请在您的onCreate方法中将R.layout.activity_main替换为R.layout.activity_navigationdrawer

英文:

As per your code , it looks like some issue with layout file use . In Activity , you have used R.layout.activity_main and code you have pasted here is activity_navigationdrawer.xml so kindly replace R.layout.activity_main with R.layout.activity_navigationdrawer in your onCreate method

huangapple
  • 本文由 发表于 2023年5月13日 20:52:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定