尝试检索的LinearLayout怪时的NullPointerException [英] Strange NullpointerException when trying to retrieve LinearLayout

查看:191
本文介绍了尝试检索的LinearLayout怪时的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个位在黑暗中拍摄的,但也许我正在做一个明显的错误;
我在main.xml中定义的LinearLayout中,一个ID@ + ID / imageListContainer。有没有别的在我main.xml中就是和的LinearLayout是完全纯(像Eclipse插入为您从GUI编辑器)。

This is a bit of a shot in the dark, but perhaps I'm making an obvious mistake; I have a LinearLayout defined in my main.xml, with an id "@+id/imageListContainer". There is nothing else in my main.xml, and the LinearLayout is completely plain (As eclipse inserts it for you from the GUI editor).

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

然后我要动态填充的LinearLayout,使用这种code:

I then want to populate the LinearLayout dynamically, using this code:

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

从withhin应用程序的onCreate()回调函数。

from withhin the applications onCreate() callback function.

这用于正常工作。然而,正如我开始添加位和code片在这里和那里(主要是在其他活动),在某些时候它停止了工作,现在我一直得到由findViewById返回的空指针()。我试图重新生成R.java,重命名的LinearLayout(以确保我已经从那里被访问控制,但它仅从这一个位置访问)。我试图把code为在onStart()代替,认为可能的onCreate跑太早了,创造了竞争条件,但没有任何运气。我还确保所述的LinearLayout确实称为imageListContainer,它是(否则生成R.java将是错误的和javac将无论如何抱怨),并且不存在重复的imageListContainer对象的任何地方。

This used to work fine. However, as I started to add bits and pieces of code here and there (mainly in other activities), at some point it stopped working, and now I consistently get returned a nullpointer by findViewById(). I tried re-generating R.java, renaming the LinearLayout (to make sure I had control from where it was accessed; but it's only accessed from this one single place). I tried putting the code into onStart() instead, thinking that onCreate might run too early, creating a race-condition, but no luck with that either. I also made sure that the LinearLayout is indeed called "imageListContainer", which it is (otherwise the generated R.java would be wrong and javac would complain anyway) and that there is no duplicate "imageListContainer" object anywhere.

我是pretty失去了很多,以什么这样做的原因可能是,所以我最好的AP preciate任何建议或猜测。

I'm pretty much lost as to what the cause of this could be, so I'd appreciate any suggestions or guesses.

下面是堆栈跟踪:

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),所以这不是问题或者

PS: I forgot to mention, I also called setContentView(R.layout.main), so that's not the problem either.

PSS:我尝试添加一个按钮,而不是图形用户界面,然后调用包含回调该按钮里面的findViewById()的code和的作品!这使我回的想法,可能会有一些竞争条件怎么回事。

PSS: I tried adding a button to the GUI instead, and then calling the code containing the findViewById() inside the callback for that button, and that works! This leads me back to the idea that there might be some race-condition going on.

推荐答案

这是从问题的意见的回答是:

This was the answer from the comments of the question:

你有你的活动一个内部类?我读了异常
  仔细跟踪:第一
   com.company.PictureListPictureListActivity.onCreate ,然后
   com.company.PictureListView.PictureListActivity.updateMainActivityScreen
   - >我看来findViewById被称为在一个不同的对象。尝试移动findViewByID到的onCreate并检查它是否在那里工作。

Do you have an inner class in your activity? I read the exception trace carefully: first com.company.PictureListPictureListActivity.onCreate then com.company.PictureListView.PictureListActivity.updateMainActivityScreen --> seems to me findViewById is called in a different object. Try moving the findViewByID into the onCreate and check if it works there.

似乎有在某一点的发展,后来去掉一个内部类。不知怎的,项目文件没有适当地更新。清洁工程,刷新文件系统的内容,并重新建设,该项目应便又做的事情。

It seems there was an inner class at some point of development, which was later removed. Somehow project files were not updated appropriately. Cleaning the project, refreshing file system contents, and re-building the project should make things right again.

这篇关于尝试检索的LinearLayout怪时的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆