简体   繁体   English

findViewById返回null TextView

[英]findViewById returning null TextView

This one is driving me and my team completely crazy. 这个让我和我的团队完全疯狂。

We can't reproduce as it works well on all our test devices but we are getting thousands of crash reports complaining of java.lang.NullPointerException for a TextView which is obtained by findViewById in onCreate (after calling setContentView and with the correct id and layout file) 我们无法重现,因为它在我们所有的测试设备上运行良好,但我们收到数千个崩溃报告,抱怨java.lang.NullPointerException是一个TextView,它是由onCreate中的findViewById获得的(在调用setContentView并具有正确的id和布局之后)文件)

java.lang.RuntimeException: Unable to start activity ComponentInfo{xxxxxxxxxxxxx.activities.PremiumActivity}: java.lang.NullPointerException
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
       at android.app.ActivityThread.access$600(ActivityThread.java:140)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4895)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.NullPointerException
       at xxxxxxxxx.activities.PremiumActivity.onCreate(PremiumActivity.java:258)
       at android.app.Activity.performCreate(Activity.java:5163)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
       at android.app.ActivityThread.access$600(ActivityThread.java:140)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4895)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
       at dalvik.system.NativeStart.main(NativeStart.java)

Here is the onCreate method: 这是onCreate方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.premium_activity_layout);
    premiumInfoHeaderTextView = (TextView) findViewById(R.id.premiumHeaderTextView);
    int themeColor = MPThemeManager.getSharedInstance(this).themeColor();
    premiumInfoHeaderTextView.setTextColor(themeColor);

That last line is the one causing the nullPointerException (line 258) 最后一行是导致nullPointerException的那一行(第258行)

Not sure if it can be related but the activity extends SherlockFragmentActivity 不确定它是否可以相关,但活动扩展了SherlockFragmentActivity

And of course premiumInfoHeaderTextView is declared as a TextView outside the onCreate method as we also use it elsewhere. 当然,premiumInfoHeaderTextView在onCreate方法之外被声明为TextView,因为我们也在其他地方使用它。

A crazy fix to a crazy bug!! 疯狂修复疯狂的错误!

As expected in my earlier comment, the bug comes from the fact the an old layout xml file for tablets (under /res/layout-large) has beed removed in our latest version (we now use a single layout file under /res/layout). 正如我在之前的评论中所预料的那样,该错误来自于我们最新版本中已删除平板电脑的旧布局xml文件(位于/ res / layout-large下)(我们现在使用/ res / layout下的单个布局文件) )。

Remember the line causing the Null Exception: 记住导致Null异常的行:

premiumInfoHeaderTextView = (TextView) findViewById(R.id.premiumHeaderTextView);

This is due to the fact that our former version layout did not have any view with id = premiumHeaderTextView and that on some tablets for users who upgraded from the previous to the new version, the old layout file has not been deleted and the system is therefore looking for id in the old /res/layout-large/premium_activity_layout.xml rather than in the new (common to all devices) /res/layout/premium_activity_layout.xml 这是因为我们以前的版本布局没有id = premiumHeaderTextView的任何视图,而且在某些平板电脑上,对于从上一个版本升级到新版本的用户,旧布局文件尚未删除,因此系统是在旧的/res/layout-large/premium_activity_layout.xml中寻找id,而不是在新的(所有设备通用)中查找id /res/layout/premium_activity_layout.xml

Using crashlytics to understand that all crashes were happening on tablet devices was key to identify this "bug" which seems like a system bug on some tablets as they should not keep old resource files when the apk is upgrade. 使用crashlytics了解平板电脑设备上发生的所有崩溃是确定这个“错误”的关键,这似乎是某些平板电脑上的系统错误,因为他们不应该在升级apk时保留旧的资源文件。

So in summary, I copied the /res/layout/premium_activity_layout.xml to /res/layout-large in order to force tablet users to use the new layout and not an old "cached" version. 总而言之,我将/res/layout/premium_activity_layout.xml复制到/ res / layout-large,以强制平板电脑用户使用新的布局而不是旧的“缓存”版本。 That has completely fixed the bug 这完全解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM