尝试显示其他活动时出现NullPointerException错误

时间:2020-11-04 19:22:11

标签: android-studio nullpointerexception

错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aplicativostartup, PID: 9927
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aplicativostartup/com.example.aplicativostartup.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 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:7860)
    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:1075)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.example.aplicativostartup.MainActivity.onCreate(MainActivity.java:55)
    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:7860) 
    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:1075) 

MainActivity:

package com.example.aplicativostartup;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.widget.Button;

import com.example.aplicativostartup.ui.gallery.ListaActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;
/* private Button buttonFrios;
private Button buttonMassas;
private Button buttonHigiene;*/




@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
            .setAction("Action", null).show());
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

    Button ButtonCarnes = findViewById(R.id.ButtonCarnes);
    ButtonCarnes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), ListaActivity.class);
            startActivity(intent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}
}

我正在做AndroidStudio中的这个项目,只是作为一个旁注,我真的需要帮助

按钮的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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"
tools:context=".ui.gallery.GalleryFragment">

<Button
    android:id="@+id/ButtonCarnes"
    android:layout_width="210dp"
    android:layout_height="50dp"
    android:layout_marginBottom="4dp"
    android:text="carnes"
    app:layout_constraintBottom_toTopOf="@+id/buttonFrios"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_chainStyle="packed" />

<Button
    android:id="@+id/buttonFrios"
    android:layout_width="210dp"
    android:layout_height="50dp"
    android:layout_marginBottom="4dp"
    android:text="frios"
    app:layout_constraintBottom_toTopOf="@+id/buttonMassas"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/ButtonCarnes" />

<Button
    android:id="@+id/buttonMassas"
    android:layout_width="210dp"
    android:layout_height="50dp"
    android:layout_marginBottom="4dp"
    android:text="massas"
    app:layout_constraintBottom_toTopOf="@+id/buttonHigiene"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/buttonFrios" />

<Button
    android:id="@+id/buttonHigiene"
    android:layout_width="210dp"
    android:layout_height="50dp"
    android:layout_marginBottom="452dp"
    android:text="higiene"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/buttonMassas" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="201dp"
    android:layout_height="50dp"
    android:layout_marginBottom="4dp"
    android:scaleType="centerCrop"
    android:src="@drawable/carnes"
    app:layout_constraintBottom_toTopOf="@+id/imageView4"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_chainStyle="packed" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" />

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="201dp"
    android:layout_height="50dp"
    android:layout_marginBottom="4dp"
    android:scaleType="centerCrop"
    android:src="@drawable/frios"
    app:layout_constraintBottom_toTopOf="@+id/imageView5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView3" />

<ImageView
    android:id="@+id/imageView5"
    android:layout_width="201dp"
    android:layout_height="50dp"
    android:layout_marginBottom="4dp"
    android:scaleType="centerCrop"
    android:src="@drawable/massas"
    app:layout_constraintBottom_toTopOf="@+id/imageView6"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView4" />

<ImageView
    android:id="@+id/imageView6"
    android:layout_width="201dp"
    android:layout_height="50dp"
    android:layout_marginBottom="452dp"
    android:scaleType="centerCrop"
    android:src="@drawable/unnamed"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView5" />
</androidx.constraintlayout.widget.ConstraintLayout>

程序甚至在打开之前就崩溃了,并且我收到NullPointerException错误,我不知道我做错了什么,但是在我搜索过的所有地方都说这是因为有一个空对象或某种类似的东西,但是我是新来的而且不知道这意味着什么或如何解决

0 个答案:

没有答案