. lang。RuntimeException:无法启动activity ComponentInfo Android Eclipse。

时间:2022-09-14 20:51:04

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中创建一个计算器,但我总是得到一个错误列表。程序注意到的文件中没有错误,尽管布局中有错误。但它之前并没有造成问题,所以不应该引起问题。

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.access$600(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);
        }


    }
}

2 个解决方案

#1


4  

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

第60行:x = (Integer) null;这一行将编译为这个字节码(由javap分解):

  1. aconst_null
  2. aconst_null
  3. checkcast #2; //class java/lang/Integer
  4. checkcast # 2;/ /类的java / lang /整数
  5. invokevirtual #3; //Method java/lang/Integer.intValue:()I
  6. invokevirtual # 3;/ / java / lang / Integer.intValue方法:我()

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. null is 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 null state.

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

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

附注:不要将零投给任何东西:)

#2


3  

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.

onCreate()无法完成,因为nullPointerException。

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

#1


4  

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

第60行:x = (Integer) null;这一行将编译为这个字节码(由javap分解):

  1. aconst_null
  2. aconst_null
  3. checkcast #2; //class java/lang/Integer
  4. checkcast # 2;/ /类的java / lang /整数
  5. invokevirtual #3; //Method java/lang/Integer.intValue:()I
  6. invokevirtual # 3;/ / java / lang / Integer.intValue方法:我()

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. null is 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 null state.

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

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

附注:不要将零投给任何东西:)

#2


3  

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.

onCreate()无法完成,因为nullPointerException。

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