这是在黑暗中拍摄的,但也许我犯了一个明显的错误; 我有一个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()的代码,并且工作正常!这让我回到了可能会出现一些种族情况的想法。
你打电话给setContentView(R.layout.main)吗?如果没有,则必须使用LayoutInflater充填xml布局 – kyp
您是否在活动中调用了“setContentView”? –
这就是你在main.xml中得到的所有东西吗?你可能会在第一行和'xmlns:android =“http://schemas.android中缺少'<?xml version =”1.0“encoding =”utf-8“?>'。com/apk/res/android“'如果是这样的话,如果是这样的话,如果没有的话,给我们看看你的'onCreate'和'updateMainActivityScreen'。 –