java.lang.RuntimeException: 无法启动活动 ComponentInfo Android Eclipse

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11722833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:09:16  来源:igfitidea点击:

java.lang.RuntimeException: Unable to start activity ComponentInfo Android Eclipse

javaandroiderror-handling

提问by Chris

I know a bunch of people have asked this same thing but I just don't know what's going on. I'm trying to make a calculator in Eclipse, but I keep getting a list of errors. There are no errors in the file that the program notices, although there is an error in the layout.xml but it hasn't caused a problem before so that shouldn't cause a problem.

我知道很多人都问过同样的问题,但我只是不知道发生了什么。我正在尝试在 Eclipse 中制作一个计算器,但我不断收到错误列表。程序注意到的文件中没有错误,虽然layout.xml中有错误,但之前没有引起问题,所以应该不会引起问题。

07-30 08:19:50.470: D/AndroidRuntime(2071): Shutting down VM
07-30 08:19:50.470: W/dalvikvm(2071): threadid=1: thread exiting with uncaught
exception (group=0x40a421f8)
07-30 08:19:50.480: E/AndroidRuntime(2071): FATAL EXCEPTION: main
07-30 08:19:50.480: E/AndroidRuntime(2071): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.se.miun.chris.calculator/com.example.se.miun.chris.
calculator.MainActivity}: java.lang.NullPointerException
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
android.app.ActivityThread.access0(ActivityThread.java:123)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
android.os.Handler.dispatchMessage(Handler.java:99)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
android.os.Looper.loop(Looper.java:137)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
android.app.ActivityThread.main(ActivityThread.java:4424)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
java.lang.reflect.Method.invokeNative(Native Method)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
java.lang.reflect.Method.invoke(Method.java:511)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
dalvik.system.NativeStart.main(Native Method)
07-30 08:19:50.480: E/AndroidRuntime(2071): Caused by: java.lang.NullPointerException
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
com.example.se.miun.chris.calculator.MainActivity.onCreate(MainActivity.java:60)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
android.app.Activity.performCreate(Activity.java:4465)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-30 08:19:50.480: E/AndroidRuntime(2071):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-30 08:19:50.480: E/AndroidRuntime(2071):     ... 11 more

This is my coding. It doesn't really do anything yet, but I wanted to just run it to see if it encountered any errors. This is the mainActivity.java file.

这是我的编码。它实际上还没有做任何事情,但我只想运行它看看它是否遇到任何错误。这是 mainActivity.java 文件。

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
Button Seven;
Button Eight;
Button Nine;
Button Four;
Button Five;
Button Six;
Button One;
Button Two;
Button Three;
Button Zero;
Button Point;
Button Negative;
TextView TextBox;
int x;
int y;
String z;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Seven = (Button)findViewById(R.id.NumberSeven);
    Seven.setOnClickListener(this);
    Eight = (Button)findViewById(R.id.NumberEight);
    Eight.setOnClickListener(this);
    Nine = (Button)findViewById(R.id.NumberNine);
    Nine.setOnClickListener(this);
    Four = (Button)findViewById(R.id.NumberFour);
    Four.setOnClickListener(this);
    Five = (Button)findViewById(R.id.NumberFive);
    Five.setOnClickListener(this);
    Six = (Button)findViewById(R.id.NumberSix);
    Six.setOnClickListener(this);
    One = (Button)findViewById(R.id.NumberOne);
    One.setOnClickListener(this);
    Two = (Button)findViewById(R.id.NumberTwo);
    Two.setOnClickListener(this);
    Three = (Button)findViewById(R.id.NumberThree);
    Three.setOnClickListener(this);
    Zero = (Button)findViewById(R.id.NumberZero);
    Zero.setOnClickListener(this);
    Point = (Button)findViewById(R.id.Point);
    Point.setOnClickListener(this);
    Negative = (Button)findViewById(R.id.NNegative);
    Negative.setOnClickListener(this);
    TextBox = (TextView)findViewById(R.id.Screen);
    x = (Integer) null;
    y = (Integer) null;


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onClick(View One) {
    if(z == null){
        x = 1;
            TextBox.setText(x);
            TextBox.setText("diggity");
        }
        else if(z != null) {
            y = 1;
            TextBox.setText(x);
            TextBox.setText(z);
            TextBox.setText(y);
        }


    }
}

回答by Psimage

line 60: x = (Integer) null; This line will compile to this bytecode (disassembled by javap):

第 60 行:x =(整数)空;这一行将编译为这个字节码(由 javap 反汇编):

  1. aconst_null
  2. checkcast#2; //class java/lang/Integer
  3. invokevirtual#3; //Method java/lang/Integer.intValue:()I
  1. aconst_null
  2. 校验#2; //类 java/lang/Integer
  3. invokevirtual #3; //方法java/lang/Integer.intValue:()I

Third line will cause a NullPointerException becouse Integer object is actually your null constant :)

第三行将导致 NullPointerException,因为 Integer 对象实际上是您的 null 常量:)

Primitive data types (int, long etc.) is the only non-object types in Java. nullis used to show that the current variable (Object variable) is not backed by the actual object (no memory was allocated). For primitive types memory allocates immediately so they cant have this nullstate.

原始数据类型(int、long 等)是 Java 中唯一的非对象类型。null用于表示当前变量(Object 变量)没有被实际对象支持(没有分配内存)。对于原始类型,内存会立即分配,因此它们不能具有此状态。

So you should check for "if(x == 0)" or define it as Integer.

因此,您应该检查“if(x == 0)”或将其定义为整数。

P.S. And don't cast null to anything :)

PS 并且不要将 null 转换为任何内容:)

回答by Shark

It's like this

就像这样

you're not getting much errors because the application can't launch. it cannot launch because it's onCreate() cannot finish.

由于应用程序无法启动,您不会收到太多错误。它无法启动,因为它的 onCreate() 无法完成。

onCreate() cannot finish because of a nullPointerException.

由于 nullPointerException,onCreate() 无法完成。

you cast null into integer twice, instead of simply instantiating a new integer which will default to 0. once you get rid of that, it should work.

你将 null 转换为整数两次,而不是简单地实例化一个默认为 0 的新整数。一旦你摆脱了它,它应该可以工作。

see?

看?

E/AndroidRuntime(2071): Caused by: java.lang.NullPointerException
E/AndroidRuntime(2071):     at
MainActivity.onCreate(MainActivity.java:60)

and i bet that this is line 60

我敢打赌这是第 60 行

x = (Integer) null;

so change it to

所以把它改成

x = new Integer();