2012-01-02 67 views
2

我正在處理的應用程序中出現致命錯誤。日誌文件和代碼如下。從我可以告訴它甚至沒有進入主要活動。Android致命錯誤「無法實例化活動」

日誌文件:

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

     }); 

    } 

在此之後有更多的代碼,但保存字符我沒有張貼。如果你需要它,請在評論中告訴我。

回答

5

在其他地方初始化您的按鈕,如onCreate


問題是this「秒(A3Activity)狀態下,當Button實例在實例變量初始化(closeAddEventButtonsubmitAddEventButton)被實例化。

實例變量初始化器在構造函數之前運行,但是一個對象沒有完全初始化,直到它的構造函數被執行。在此之前引用的對象將處於不確定狀態。

(狀態可以被確定;我僅僅意味着該實例可能不完全可用尚未。)

東西在Button初始化流程使用從A3Activity一個未初始化的值並引發NPE。 (JLS 12.5: Creation of New Class Instances細節實例的初始化過程)

注意到初始化的文字順序(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) 

這是我真正看到了唯一的日誌行,因爲我的第一步是尋找和外部參考什麼roid和Java包。 <init>部分表示它不在方法中,所以它與實例的初始化器有關。

堆棧跟蹤包括Button初始值設定項。再加上在建設者中使用this意味着這是唯一真正的可能性,因此在沒有多少挖掘的情況下快速轉身。

+0

它工作,但爲什麼我不能這樣做,以及如何將一個onClick事件分配給動態按鈕? - 太感謝了! – Reid 2012-01-02 07:05:11

相關問題