0

I'm creating an app for a sensor and when I start it the app crashes.

I tried the app on 2 different phones and 2 emulators but crashes un every device.

Here is the MainActivity.java file:::

package com.example.ir_sensor;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    openMenu = findViewById(R.id.Open_menu);
    closeMenu = findViewById(R.id.Close_menu);
}

View openMenu = findViewById(R.id.Open_menu);
View closeMenu = findViewById(R.id.Close_menu);

public void menu() {
    if (openMenu.getVisibility() == View.VISIBLE) {
        closeMenu.setVisibility(View.GONE);
    } else if (closeMenu.getVisibility() == View.GONE){
        openMenu.setVisibility(View.VISIBLE);
    }
}

}

Here is the activity_main.xml file:::

<?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">

<ImageView
    android:id="@+id/LOGO"
    android:layout_width="0dp"
    android:layout_height="248dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="52dp"
    android:layout_marginEnd="8dp"
    android:layout_weight="1"
    android:contentDescription="@string/logo"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/logo_int_lukka" />

<ImageView
    android:id="@+id/Close_menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="49dp"
    android:visibility="gone"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/LOGO"
    app:srcCompat="@drawable/ic_menu_less" />

<ImageView
    android:id="@+id/Open_menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="49dp"
    android:contentDescription="@string/openmenu"
    android:visibility="visible"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/LOGO"
    app:srcCompat="@android:drawable/ic_menu_more" />

<LinearLayout
    android:id="@+id/menuLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="48dp"
    android:layout_marginTop="380dp"
    android:baselineAligned="false"
    android:orientation="horizontal"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/houseMenu"
            android:layout_width="4dp"
            android:layout_height="70dp"
            android:layout_weight="1"
            android:contentDescription="@string/housemenu"
            app:srcCompat="@drawable/house_menu" />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2">

        </FrameLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/IR_LED"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:layout_weight="1"
            android:contentDescription="@string/ir_led"
            app:srcCompat="@drawable/ir_led" />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2">

        </FrameLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/alarmSign"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:layout_weight="1"
            android:contentDescription="@string/alarmsign"
            app:srcCompat="@drawable/on_off__alert" />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2">

        </FrameLayout>

    </LinearLayout>

</LinearLayout>

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/gridline380FromTop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="380dp"
    android:orientation="horizontal"
    app:layout_constraintGuide_begin="380dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/gridline48FromLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="48dp"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="48dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is the Logcat:::

2019-08-12 17:14:39.966 9648-9648/com.example.ir_sensor E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ir_sensor, PID: 9648 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ir_sensor/com.example.ir_sensor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 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:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at androidx.appcompat.app.AppCompatDelegateImpl.(AppCompatDelegateImpl.java:249) at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182) at androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520) at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:191) at com.example.ir_sensor.MainActivity.(MainActivity.java:18) at java.lang.Class.newInstance(Native Method) at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95) at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43) at android.app.Instrumentation.newActivity(Instrumentation.java:1243) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)  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:2016)  at android.os.Handler.dispatchMessage(Handler.java:107)  at android.os.Looper.loop(Looper.java:214)  at android.app.ActivityThread.main(ActivityThread.java:7356)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)  What can I do?

Lukka
  • 29
  • 9
  • `java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at` there's your error – Zun Aug 12 '19 at 09:46
  • If any answer helped you, please select one of them. – Gaurav Mall Aug 12 '19 at 12:28

1 Answers1

-1

The error is that you are defining the openMenu and closeMenu variables, outside the onCreate() method. The thing is however that before onCreate you haven't defined your view, so getting the view by id before the view has been defined results in a NullPointerException (the program can't find the variable you were asking for and crashes). So do this:

package com.example.ir_sensor;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        openMenu = findViewById(R.id.Open_menu);
        closeMenu = findViewById(R.id.Close_menu);
    }

    View openMenu;
    View closeMenu;

    public void menu() {
        if (openMenu.getVisibility() == View.VISIBLE) {
            closeMenu.setVisibility(View.GONE);
        } else if (closeMenu.getVisibility() == View.GONE){
            openMenu.setVisibility(View.VISIBLE);
        }
    }
}
Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33