2

作業中のアプリケーションで致命的なエラーが発生しました。ログファイルとコードは次のとおりです。私が言えることから、それはメインの活動にさえ到達していません。

ログ ファイル:

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

        });

    }

この後もコードがありますが、文字を保存するために投稿していません。必要な方はコメントにてお知らせください。

4

1 に答える 1

5

のように、他の場所でボタンを初期化しますonCreate


問題は、インスタンスがインスタンス変数初期化子 (および) でインスタンス化されるときthisの ( A3Activity) の状態です。ButtoncloseAddEventButtonsubmitAddEventButton

インスタンス変数初期化子はコンストラクターの前に実行されますが、コンストラクターが実行されるまでオブジェクトは完全に初期化されません。それより前に参照されたオブジェクトは不確定な状態になります。

(状態は判断できます。つまり、インスタンスがまだ完全に使用可能ではない可能性があるということです。)

初期化フローの何かが からの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 パッケージ以外への参照を探すことだったので、これは私が実際に見た唯一のログ行です。その<init>部分は、メソッドに含まれていなかったことを意味するため、インスタンスの初期化子と関係がありました。

スタック トレースにButtonイニシャライザ エントリが含まれていました。それに加えてthis、コンストラクターでの使用は、それが唯一の現実的な可能性であることを意味し、したがって、あまり掘り下げることなく迅速なターンアラウンドを実現しました。

于 2012-01-02T04:37:42.497 に答える