如何解决Android Studio中的崩溃问题

时间:2019-08-12 09:37:23

标签: java android

我正在为传感器创建一个应用程序,当我启动它时,该应用程序崩溃了。

  

我在2个不同的手机和2个模拟器上尝试了该应用程序,但每个设备都崩溃了。

这是MainActivity.java文件:::

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

}

这是activity_main.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">

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

这是Logcat :::

  

2019-08-12 17:14:39.966 9648-9648 / com.example.ir_sensor E / Android运行时:致命异常:主要       流程:com.example.ir_sensor,PID:9648       java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.ir_sensor / com.example.ir_sensor.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.view.Window $ Callback android.view。空对象引用上的Window.getCallback()'           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2016)           在android.os.Handler.dispatchMessage(Handler.java:107)           在android.os.Looper.loop(Looper.java:214)           在android.app.ActivityThread.main(ActivityThread.java:7356)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)        原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.Window $ Callback android.view.Window.getCallback()'           在androidx.appcompat.app.AppCompatDelegateImpl。(AppCompatDelegateImpl.java:249)           在androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182)           在androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)           在androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)           在com.example.ir_sensor.MainActivity。(MainActivity.java:18)           在java.lang.Class.newInstance(本地方法)           在android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)           在androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)           在android.app.Instrumentation.newActivity(Instrumentation.java:1243)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2016)           在android.os.Handler.dispatchMessage(Handler.java:107)           在android.os.Looper.loop(Looper.java:214)           在android.app.ActivityThread.main(ActivityThread.java:7356)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)   我该怎么办?

1 个答案:

答案 0 :(得分:-1)

错误是您在openMenu方法之外定义了closeMenuonCreate()变量。但是问题是,在onCreate之前,您还没有定义视图,因此在定义视图之前通过id获取视图会导致NullPointerException(程序无法找到您要的变量并导致崩溃)。这样做:

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