1

這是在黑暗中拍攝的,但也許我犯了一個明顯的錯誤; 我有一個LinearLayout在我的main.xml中定義,ID爲「@ + id/imageListContainer」。我的main.xml中沒有其他東西,LinearLayout完全是純文本的(當eclipse從GUI編輯器爲你插入時)。嘗試檢索LinearLayout時發生奇怪的NullpointerException

<LinearLayout 
     android:id="@+id/imageListContainer" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 
    </LinearLayout> 

我再要動態填充的LinearLayout,使用此代碼:

LinearLayout imageContainer = (LinearLayout)findViewById(R.id.imageListContainer); 

從withhin應用程序的onCreate()回調函數。

這用於正常工作。然而,當我開始在這裏和那裏添加代碼段時(主要是在其他活動中)​​,在某些時候它停止工作,現在我一直通過findViewById()返回一個空指針。我嘗試重新生成R.java,重命名LinearLayout(以確保我可以控制它從哪裏訪問;但它只能從這一個單獨的地方訪問)。我嘗試將代碼放入onStart()中,認爲onCreate可能運行得太早,創建了競爭條件,但沒有運氣。我還確保LinearLayout實際上被稱爲「imageListContainer」,它是(否則生成的R.java將是錯誤的,並且javac會抱怨),並且任何地方都沒有重複的「imageListContainer」對象。

我非常失望,因爲這可能是什麼原因,所以我會很感激任何建議或猜測。

這裏是堆棧跟蹤:

E/AndroidRuntime( 327): Uncaught handler: thread main exiting due to uncaught exception 
E/AndroidRuntime( 327): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.PictureListView/com.company.PictureListView.PictureListView.PictureListActivity}: java.lang.NullPointerException 
E/AndroidRuntime( 327):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
E/AndroidRuntime( 327):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
E/AndroidRuntime( 327):  at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
E/AndroidRuntime( 327):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
E/AndroidRuntime( 327):  at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime( 327):  at android.os.Looper.loop(Looper.java:123) 
E/AndroidRuntime( 327):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
E/AndroidRuntime( 327):  at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime( 327):  at java.lang.reflect.Method.invoke(Method.java:521) 
E/AndroidRuntime( 327):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
E/AndroidRuntime( 327):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
E/AndroidRuntime( 327):  at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime( 327): Caused by: java.lang.NullPointerException 
E/AndroidRuntime( 327):  at com.company.PictureListView.PictureListActivity.updateMainActivityScreen(PictureListActivity.java:50) 
E/AndroidRuntime( 327):  at com.company.PictureListPictureListActivity.onCreate(PictureListActivity.java:38) 
E/AndroidRuntime( 327):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
E/AndroidRuntime( 327):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 

PS:我忘了提,我也叫的setContentView(R.layout.main),所以,要麼是沒有問題的。

PSS:我嘗試在GUI中添加一個按鈕,然後調用包含該按鈕的回調函數內的findViewById()的代碼,並且工作正常!這讓我回到了可能會出現一些種族情況的想法。

+1

你打電話給setContentView(R.layout.main)嗎?如果沒有,則必須使用LayoutInflater充填xml佈局 – kyp

+1

您是否在活動中調用了「setContentView」? –

+1

這就是你在main.xml中得到的所有東西嗎?你可能會在第一行和'xmlns:android =「http://schemas.android中缺少'<?xml version =」1.0「encoding =」utf-8「?>'。com/apk/res/android「'如果是這樣的話,如果是這樣的話,如果沒有的話,給我們看看你的'onCreate'和'updateMainActivityScreen'。 –

回答

3

這是從問題的評論答案:

你有你的活動一個內部類?我仔細看了例外 跟蹤:第一 com.company.PictureListPictureListActivity.onCreate然後 com.company.PictureListView.PictureListActivity.updateMainActivityScreen - >我看來findViewById被稱爲在不同的對象。嘗試將findViewByID移動到onCreate並檢查它是否在那裏工作。

似乎在某個開發階段有一個內部類,後來被刪除。不知何故項目文件沒有適當更新。清理項目,刷新文件系統內容以及重新構建項目應該讓事情重演。

+1

爲了澄清,我告訴日食按照您的建議清理項目後,問題就消失了。我不確定究竟是什麼問題,但它可能是一個嵌套類繼承的.class文件。我稍後會仔細研究這個問題,因爲現在我很樂意回到正軌。謝謝你的時間,並有我所有的讚揚。 – Amadiro

+2

好的,很好:)我注意到,每當資源被修改時,就有可能出現錯誤。修改資源後,我總是清理+刷新+生成項目。自採用這種方式以來從未遇到過問題 –

相關問題