Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法启动活动ComponentInfo java.lang.NullPointerException:尝试对空对象引用调用虚拟方法_Java_Android_Nullpointerexception - Fatal编程技术网

无法启动活动ComponentInfo java.lang.NullPointerException:尝试对空对象引用调用虚拟方法

无法启动活动ComponentInfo java.lang.NullPointerException:尝试对空对象引用调用虚拟方法,java,android,nullpointerexception,Java,Android,Nullpointerexception,我正试图用一个按钮切换到另一个活动。第二个活动(称为capturar)拍摄一张照片并保存在gallery中。 但是,出于某种原因,每次我试图用按钮切换到那个活动时,它都会崩溃,我知道问题出在捕获者的代码上,因为如果我对所有代码都进行注释,它就会工作 我要把我的主要活动代码的图片(我试图有目的地更改为其他活动)和我的Capturar活动的代码放在我试图拍照并保存的地方 主要活动: package com.iris.phototask; import androidx.annotation.Nul

我正试图用一个按钮切换到另一个活动。第二个活动(称为capturar)拍摄一张照片并保存在gallery中。 但是,出于某种原因,每次我试图用按钮切换到那个活动时,它都会崩溃,我知道问题出在捕获者的代码上,因为如果我对所有代码都进行注释,它就会工作

我要把我的主要活动代码的图片(我试图有目的地更改为其他活动)和我的Capturar活动的代码放在我试图拍照并保存的地方

主要活动:

package com.iris.phototask;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    //Declaramos las variables
    public Button btnHorario;
    public Button Galeria;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Asignamos el componente al que corresponde la variable
        btnHorario = (Button) findViewById(R.id.buttonHorario);
        ImageButton imgButtonCamara = findViewById(R.id.imageButtonCamera);
        Galeria = (Button) findViewById(R.id.buttonGaleria);


        //Creamos un onClickListener para generar un cambio de actividad al pulsar el botón
       btnHorario.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Creamos un Intent que nos cambie de ventana
                Intent intent = new Intent (MainActivity.this, Horario.class);
                startActivity(intent);

            }
        });

        imgButtonCamara.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Creamos un Intent que nos cambie de ventana
                Intent intent = new Intent (MainActivity.this, Capturar.class);
                startActivity(intent);

            }
        });
    }
}

主活动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=".MainActivity">

    <Button
        android:id="@+id/buttonGaleria"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="30dp"
        android:background="#8BC34A"
        android:text="Galeria"
        app:layout_constraintBottom_toTopOf="@+id/buttonHorario"
        app:layout_constraintEnd_toEndOf="@+id/buttonHorario"
        app:layout_constraintStart_toStartOf="@+id/buttonHorario"
        app:layout_constraintTop_toBottomOf="@+id/imageButtonCamera" />

    <Button
        android:id="@+id/buttonHorario"
        android:layout_width="128dp"
        android:layout_height="0dp"
        android:layout_marginBottom="234dp"
        android:background="#8BC34A"
        android:text="Horario"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonGaleria" />

    <ImageButton
        android:id="@+id/imageButtonCamera"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="175dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="32dp"
        android:background="#8BC34A"
        android:contentDescription="@string/app_name"
        app:layout_constraintBottom_toTopOf="@+id/buttonGaleria"
        app:layout_constraintEnd_toEndOf="@+id/buttonGaleria"
        app:layout_constraintStart_toStartOf="@+id/buttonHorario"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:drawable/ic_menu_camera" />


</androidx.constraintlayout.widget.ConstraintLayout>
CapturarXML

<?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=".Capturar">

    <ImageView
        android:id="@+id/imageViewImagen"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="45dp"
        android:layout_marginTop="73dp"
        android:layout_marginEnd="45dp"
        android:layout_marginBottom="73dp"
        app:layout_constraintBottom_toTopOf="@+id/buttonCapture"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:drawable/ic_menu_gallery" />

    <Button
        android:id="@+id/buttonCapture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="51dp"
        android:text="Capturar"
        android:background="#8BC34A"
        app:layout_constraintBottom_toBottomOf="@+id/buttonGuardar"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/buttonGuardar" />

    <Button
        android:id="@+id/buttonGuardar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="52dp"
        android:layout_marginBottom="78dp"
        android:text="Guardar"
        android:background="#8BC34A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

非常感谢,希望我能解释清楚。

空指针异常是由于以下几行引起的

findViewById(R.id.save_local).setOnClickListener(buttonGuardar);

因为它找不到id
保存\u local

完全不是重复的
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.iris.phototask, PID: 6499
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iris.phototask/com.iris.phototask.Capturar}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6810)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.iris.phototask.Capturar.onCreate(Capturar.java:54)
        at android.app.Activity.performCreate(Activity.java:7224)
        at android.app.Activity.performCreate(Activity.java:7213)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
findViewById(R.id.save_local).setOnClickListener(buttonGuardar);