2011-07-15 241 views
0

我是Java編程新手,這讓我很難過。當我的應用程序啓動時,我得到一個運行時異常和一個關閉力。它在我將很多代碼寫入數組而不是單打之前確實有效。有人能給我一個線索可能是錯的嗎?運行時異常

07-15 16:30:29.441: INFO/ARMAssembler(61): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x38fe50:0x390018] in 1735976 ns 
07-15 16:30:29.551: ERROR/gralloc(61): [unregister] handle 0x4910f0 still locked (state=40000001) 
07-15 16:30:58.871: DEBUG/AndroidRuntime(229): Shutting down VM 
07-15 16:30:58.871: WARN/dalvikvm(229): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
07-15 16:30:58.871: ERROR/AndroidRuntime(229): Uncaught handler: thread main exiting due to uncaught exception 
07-15 16:30:59.252: ERROR/AndroidRuntime(229): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.golfandroid/com.example.golfandroid.GolfScoreActivity}: java.lang.NullPointerException 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at android.os.Looper.loop(Looper.java:123) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at java.lang.reflect.Method.invoke(Method.java:521) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at dalvik.system.NativeStart.main(Native Method) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229): Caused by: java.lang.NullPointerException 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at com.example.golfandroid.GolfScoreActivity.SetupClickListeners(GolfScoreActivity.java:725) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at com.example.golfandroid.GolfScoreActivity.onCreate(GolfScoreActivity.java:551) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
07-15 16:30:59.252: ERROR/AndroidRuntime(229):  ... 11 more 
07-15 16:30:59.432: INFO/Process(61): Sending signal. PID: 229 SIG: 3 
07-15 16:30:59.462: INFO/dalvikvm(229): threadid=7: reacting to signal 3 
07-15 16:30:59.753: INFO/dalvikvm(229): Wrote stack trace to '/data/anr/traces.txt' 

代碼:

public void SetupClickListeners(){ 
    for(int holeNum=1; holeNum<18; holeNum++){ 
     scorePlayHole[currentPlayerNum-1][holeNum-1].addTextChangedListener(this); 
    } 
    for(int i=1; i<currentPlayerNum*4; i++){ 
     NextPlayerHoleButton[currentPlayerNum-1][i].setOnClickListener(this); 
    } 
} 
+1

你是如何創建數組? 「NullPointerException」意味着你試圖使用一個空值的引用。既然你說過直到你開始使用數組纔會發生,我猜你沒有初始化它們。 – DeeV

+2

其實我認爲空指針異常是在725行(GolfScoreActivity.SetupClickListeners(GolfScoreActivity.java:725))...因此,很高興看到(說)從551到725 +/- ... – Torid

+0

public void SetupClickListeners(){(0; holeNum = 1; holeNum <18; holeNum ++){ scorePlayHole [currentPlayerNum-1] [holeNum-1] .addTextChangedListener(this); } 對(INT I = 1;我 John

回答

1

這僅僅是一個在黑暗中刺,因爲我只有堆棧跟蹤和不相關的來源。

您是否看了這個源文件? GolfScoreActivity.java 行號725

看起來要設置一個點擊監聽器,但事情並沒有初始化,是空

+0

你是如何離開的人原來的問題下的問題?我沒有看到在那裏發表評論的選項。是否有解鎖該功能的最低分數? –

+1

與加評論,但需要50代表 - 在這裏完整列表http://stackoverflow.com/faq#reputation – fvu

+0

宇豪,剛剛打破50,謝謝你的票了:) –

0

射擊在黑暗中沒有看到它是很難說的代碼 - 但如果你已經將代碼轉換爲使用數組,你是否記得新的數組本身,以及進入數組的對象?

+0

這是EditText上的數組,它一直在我調用此方法爲每個EditText創建一個textchangelistener之前,初始化並鏈接到ID。有沒有人喜歡志願者的電子郵件地址? – John

+0

鑑於上面粘貼的代碼片段,它在我看來像數組尚未完全初始化,並且當您嘗試檢索其中一個值時,您會返回null。你可以嘗試在for循環中添加一條日誌消息來打印索引 - 這樣你就可以看到哪個迭代導致了崩潰。這應該有助於您確定尚未初始化的內容。 – ksasq

+0

謝謝ksasq--對不起,但我該怎麼做?它會寫入控制檯還是其他地方? – John

0

07-15 16:30:59.252: ERROR/AndroidRuntime(229): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.golfandroid/com.example.golfandroid.GolfScoreActivity}: java.lang.NullPointerException

這是不是一個真正的運行時異常。這是一個包裝空指針異常的運行時異常。根本原因是一個空指針:

檢查您的代碼後,其中任何一個可能是null,因此導致空指針異常。

scorePlayHole
scorePlayHole[currentPlayerNum-1]
scorePlayHole[currentPlayerNum-1][holeNum-1]
NextPlayerHoleButton
NextPlayerHoleButton[currentPlayerNum-1]
NextPlayerHoleButton[currentPlayerNum-1][i]