Android 致命错误“无法实例化 Activity”

发布于 2024-12-23 17:26:36 字数 4565 浏览 1 评论 0原文

我正在开发的应用程序中遇到致命错误。日志文件和代码如下。据我所知,它甚至还没有进入主要活动。

日志文件:

Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x401db760)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{A3.cal/A3.cal.A3Activity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1739)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
    at android.app.ActivityThread.access$500(ActivityThread.java:122)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:132)
    at android.app.ActivityThread.main(ActivityThread.java:4123)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
    at android.view.View.<init>(View.java:2366)
    at android.view.View.<init>(View.java:2411)
    at android.widget.TextView.<init>(TextView.java:371)
    at android.widget.Button.<init>(Button.java:108)
    at android.widget.Button.<init>(Button.java:104)
    at android.widget.Button.<init>(Button.java:100)
    at A3.cal.A3Activity.<init>(A3Activity.java:32)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1301)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1730)
    ... 11 more

代码:

package A3.cal;

import java.util.Calendar;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

public class A3Activity extends Activity {
    public int currentMonth = 0;
    private TextView[] dayTableTV = new TextView[6*7]; 
    private Integer[] dayTable = null;
    public Integer currentYear = 0;
    public int firstDayOfWeek = 0;
    public boolean leapYear = false;
    private PopupWindow createEventPopup = null;
    boolean click = false;
    private Button closeAddEventButton = new Button(this);
    private Button submitAddEventButton = new Button(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        makeTable();

        Button addEvent = (Button) findViewById(R.id.addEvent);
        addEvent.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(click == false){
                    addEventPopup();
                    click = true;
                }else{
                    createEventPopup.dismiss();
                    click = false;
                }
            }
        });

        Button nextMonthButton = (Button) findViewById(R.id.nextMonthButton);
        nextMonthButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               changeMonthNext(getMonth(currentMonth, 1));
            }
        });

        Button previousMonthButton = (Button) findViewById(R.id.previousMonthButton);
        previousMonthButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               changeMonthBack(getMonth(currentMonth, -1));
            }
        });

        closeAddEventButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                createEventPopup.dismiss();
            }
        });

        submitAddEventButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               submitEvent();
            }

        });

    }

此后还有更多代码,但为了保存字符,我不会发布它。如果您需要,请在评论中告诉我。

I am getting a fatal error in an application that I have been working on. The log file and code is as follows. From what I can tell it is not even getting to the main activity.

Log File:

Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x401db760)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{A3.cal/A3.cal.A3Activity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1739)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
    at android.app.ActivityThread.access$500(ActivityThread.java:122)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:132)
    at android.app.ActivityThread.main(ActivityThread.java:4123)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
    at android.view.View.<init>(View.java:2366)
    at android.view.View.<init>(View.java:2411)
    at android.widget.TextView.<init>(TextView.java:371)
    at android.widget.Button.<init>(Button.java:108)
    at android.widget.Button.<init>(Button.java:104)
    at android.widget.Button.<init>(Button.java:100)
    at A3.cal.A3Activity.<init>(A3Activity.java:32)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1301)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1730)
    ... 11 more

Code:

package A3.cal;

import java.util.Calendar;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

public class A3Activity extends Activity {
    public int currentMonth = 0;
    private TextView[] dayTableTV = new TextView[6*7]; 
    private Integer[] dayTable = null;
    public Integer currentYear = 0;
    public int firstDayOfWeek = 0;
    public boolean leapYear = false;
    private PopupWindow createEventPopup = null;
    boolean click = false;
    private Button closeAddEventButton = new Button(this);
    private Button submitAddEventButton = new Button(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        makeTable();

        Button addEvent = (Button) findViewById(R.id.addEvent);
        addEvent.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(click == false){
                    addEventPopup();
                    click = true;
                }else{
                    createEventPopup.dismiss();
                    click = false;
                }
            }
        });

        Button nextMonthButton = (Button) findViewById(R.id.nextMonthButton);
        nextMonthButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               changeMonthNext(getMonth(currentMonth, 1));
            }
        });

        Button previousMonthButton = (Button) findViewById(R.id.previousMonthButton);
        previousMonthButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               changeMonthBack(getMonth(currentMonth, -1));
            }
        });

        closeAddEventButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                createEventPopup.dismiss();
            }
        });

        submitAddEventButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               submitEvent();
            }

        });

    }

There is more code after this, but for saving characters I am not posting it. If you need it please let me know in the comments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一直都在从未离去 2024-12-30 17:26:36

在其他地方初始化按钮,例如在 onCreate 中。


问题是在实例变量初始值设定项 (closeAddEventButton) 中实例化 Button 实例时 thisA3Activity)的状态代码>和submitAddEventButton)。

实例变量初始值设定项在构造函数之前运行,但对象在其构造函数执行之前并未完全初始化。在此之前引用的对象将处于不确定状态。

(状态可以确定;我只是说实例可能尚未完全可用。)

Button 初始化流程中的某些内容使用了 中的未初始化值A3Activity 并抛出一个 NPE。 (JLS 12.5:创建新类实例详细说明了实例初始化过程。)

请注意初始化程序按文本顺序运行(JLS 12.4.2),在构造函数之前,这是一个简单的演示:

public class ThisHere {
    public String aww;
    public String notYet = this.aww;

    GedankenExperiment wat = new GedankenExperiment(this);

    // public class GedankenExperiment {
    //     public GedankenExperiment(thatThere) {
    //         thatThere.aww = ???
    //     }
    // }

    public ThisHere() {
        this.aww = "Hi there";
    }

    public static void main(String[] args) {
        ThisHere thisHere = new ThisHere();
        System.out.println("aww=" + thisHere.aww);
        // > aww=Hi there (initialized in ctor)
        System.out.println("notYet=" + thisHere.notYet);
        // > notYet=null (initialized before ctor)
    }
}

仅供参考,明显的标志是这个日志行:

at A3.cal.A3Activity.<init>(A3Activity.java:32)

这是我真正看到的唯一日志行,因为我的第一步是寻找对 Android 和 Java 包之外的任何内容的引用。 部分意味着它不在方法中,因此它与实例的初始值设定项有关。

堆栈跟踪包括 Button 初始值设定项。再加上在构造函数中使用 this 意味着这是唯一真正的可能性,因此无需太多挖掘即可快速周转。

Initialize your buttons elsewhere, like in onCreate.


The problem is this's (the A3Activity) state when the Button instances are instantiated in the instance variable initializers (closeAddEventButton and submitAddEventButton).

Instance variable initializers run before constructors, but an object is not completely initialized until its constructor(s) are executed. Objects referenced prior to that will be in an indeterminate state.

(The state could be determined; I just mean the instance may not be fully-usable yet.)

Something in the Button initialization flow uses an uninitialized value from the A3Activity and throws an NPE. (JLS 12.5: Creation of New Class Instances details instance initialization process.)

Noting that initializers run in textual order (JLS 12.4.2), before constructors, here's a simple demo:

public class ThisHere {
    public String aww;
    public String notYet = this.aww;

    GedankenExperiment wat = new GedankenExperiment(this);

    // public class GedankenExperiment {
    //     public GedankenExperiment(thatThere) {
    //         thatThere.aww = ???
    //     }
    // }

    public ThisHere() {
        this.aww = "Hi there";
    }

    public static void main(String[] args) {
        ThisHere thisHere = new ThisHere();
        System.out.println("aww=" + thisHere.aww);
        // > aww=Hi there (initialized in ctor)
        System.out.println("notYet=" + thisHere.notYet);
        // > notYet=null (initialized before ctor)
    }
}

FYI, the tell-tale sign was this log line:

at A3.cal.A3Activity.<init>(A3Activity.java:32)

This is the only log line I really saw, because my first step was looking for a reference to anything outside of Android and Java packages. The <init> part means it wasn't in a method, so it had something to do with the instance's initializers.

The stack trace included Button initializer entries. That plus the use of this in their constructors meant that was the only real possibility, hence the quick turn-around without much digging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文