简体   繁体   English

当我单击 go 此活动时,应用程序崩溃并出现 NullPointerException 致命异常

[英]App crash with FATAL EXCEPTION with NullPointerException when I click go this activity

I created an android studio project but I found null pointer exception error and all of these error again and again when I click a button that links activity while it didn't happen to other activity who have similar code.我创建了一个 android 工作室项目,但我发现 null 指针异常错误,当我单击链接活动的按钮时,所有这些错误一次又一次地出现,而其他具有类似代码的活动却没有发生。 even I try to change something, it always shows the same error in logcat.即使我尝试更改某些内容,它也总是在 logcat 中显示相同的错误。 I quite beginner in android development我是 android 开发的新手

Here is my code这是我的代码

package com.example.chemicalculators;

import static java.lang.Integer.*;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.concurrent.atomic.AtomicReference;

public class CalCal5 extends AppCompatActivity {
    int eNo;
    String eSym;
    String eName;
    double eMass;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AtomicReference<EditText> eNoString = new AtomicReference<>((EditText) findViewById(R.id.editTextNumber));
        ImageButton searchButton = (ImageButton) findViewById(R.id.imageButton);
        Button button = (Button) findViewById(R.id.button);
        final TextView outESymbol = (TextView) findViewById(R.id.elementSymbol);
        final TextView outEName = (TextView) findViewById(R.id.elementName);
        final TextView outEMass = (TextView) findViewById(R.id.AtomicMassValue);

        searchButton.setOnClickListener(view -> {
            eNo = parseInt(eNoString.get().getText().toString());
            switch(eNo)
            {
                case 1:
                    eSym = "H";
                    eName = "Hydrogen";
                    eMass = 1.00797;
                case 2:
                    eSym = "He";
                    eName = "Helium";
                    eMass = 4.0026;
                case 3:
                    eSym = "Li";
                    eName = "Lithium";
                    eMass = 6.941;
                case 4:
                    eSym = "Be";
                    eName = "Beryllium";
                    eMass = 9.0122;
                case 5:
                    eSym = "B";
                    eName = "Boron";
                    eMass = 10.811;
                case 6:
                    eSym = "C";
                    eName = "Carbon";
                    eMass = 12.0107;
                
                default :
                    eSym = "-";
                    eName = "None";
                    eMass = 0.00;
            }

            outESymbol.setText(eSym);
            outEName.setText(eName);
            outEMass.setText(String.valueOf(eMass));

            Toast.makeText(CalCal5.this, "Finding Success", Toast.LENGTH_SHORT).show();
        });

        button.setOnClickListener(view -> {
            Intent p = new Intent(CalCal5.this, MainActivity.class);
            startActivity(p);
        });
    }
}

And this is my Logcat这是我的 Logcat

2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2022-02-14 09:50:25.414 6477-6477/com.example.chemicalculators E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.chemicalculators, PID: 6477
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chemicalculators/com.example.chemicalculators.CalCal5}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.chemicalculators.CalCal5.onCreate(CalCal5.java:33)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

You are not calling setContentView() to inflate layout您没有调用setContentView()来扩充布局

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.YOUR_LAYOUT)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试 go 到带有图像按钮的第二个活动时,应用程序崩溃 - App crash when i tried to go to the second activity with a image button E/AndroidRuntime:致命异常:应用启动时主要崩溃 - E/AndroidRuntime: FATAL EXCEPTION: main crash when app start FATAL EXCEPTION for AndroidRuntime 启动应用程序一段时间并导致崩溃 - FATAL EXCEPTION for AndroidRuntime when starting the app for a period of time and lead to crash 当我单击一个按钮以错误打开新活动时,我的应用程序崩溃:NullPointerException - My App crashes when i click a button to open new activity with error: NullPointerException 运行此应用程序时出现致命异常显示错误,无法启动活动组件 - Fatal exception show error unable to start activity component when I'm running this app 单击按钮时活动中的NullPointerException - NullPointerException in Activity when click on button 当我在活动开始时点击垃圾邮件时,为什么我的 Android Studio 应用程序会崩溃? - Why does my Android studio App crash when I spam click at the beginning of an activity? Android应用程式当机:致命异常:主要 - Android App crash: FATAL EXCEPTION: main Android - FATAL EXCEPTION:main - 无法启动活动ComponentInfo NullPointerException - Android - FATAL EXCEPTION: main - Unable to start activity ComponentInfo NullPointerException 严重异常:无法启动活动ComponentInfo {…}:java.lang.NullPointerException - FATAL EXCEPTION: Unable to start activity ComponentInfo{…}: java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM