Android 修改按钮时onResume中出现NullPointerException,为什么?

Android 修改按钮时onResume中出现NullPointerException,为什么?,android,android-ui,android-activity,android-2.1-eclair,Android,Android Ui,Android Activity,Android 2.1 Eclair,我从我的许多用户那里得到了一个NullPointerException,我无法发现问题所在,它被抛出到这行: ((按钮)findViewById(R.id.speakEnglishButton)).setText(“”) 我看不出哪里出了问题,这个按钮有那个Id,它编译得很好,在我的模拟器和2台设备上运行得很好,但是我在我的开发者控制台上为这个版本的用户发布了大约100个错误 仔细看一下简历: @Override protected void onResume() { super.onR

我从我的许多用户那里得到了一个NullPointerException,我无法发现问题所在,它被抛出到这行:

((按钮)findViewById(R.id.speakEnglishButton)).setText(“”)

我看不出哪里出了问题,这个按钮有那个Id,它编译得很好,在我的模拟器和2台设备上运行得很好,但是我在我的开发者控制台上为这个版本的用户发布了大约100个错误

仔细看一下简历:

@Override
protected void onResume()
{
    super.onResume();

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    boolean isButtonLabelsEnabled = sp.getBoolean("isButtonLabelsEnabled", false);
    if (!isButtonLabelsEnabled)
    {
        ((Button)findViewById(R.id.speakEnglishButton)).setText("");
        ((Button)findViewById(R.id.speakFrenchButton)).setText("");
        ((Button)findViewById(R.id.speakSpanishButton)).setText("");
        ((Button)findViewById(R.id.speakGermanButton)).setText("");
        ((Button)findViewById(R.id.speakItalianButton)).setText("");
    }
    else
    {
        ((Button)findViewById(R.id.speakEnglishButton)).setText(getString(R.string.btnLblEnglish));
        ((Button)findViewById(R.id.speakFrenchButton)).setText(getString(R.string.btnLblFrench));
        ((Button)findViewById(R.id.speakSpanishButton)).setText(getString(R.string.btnLblSpanish));
        ((Button)findViewById(R.id.speakGermanButton)).setText(getString(R.string.btnLblGerman));
        ((Button)findViewById(R.id.speakItalianButton)).setText(getString(R.string.btnLblItalian));
    }
}
基本上,我所做的只是检查保存的首选项布尔值,根据其值,我要么在按钮上设置标签,要么将其设置为空白

有人能给我建议吗


谢谢

很可能是因为

((Button)findViewById(R.id.speakEnglishButton));
返回null。 如果找不到这样的资源,就会发生这种情况。 我自然而然想到一个原因:布局中没有默认资源。
这意味着:您必须至少有一个“默认”布局,适用于每种语言/方向/。。。应将其放置在“布局”文件夹中。简单地说就是“布局”。

我认为关键是,当用户恢复活动时,布局没有设置,因此
findViewById()
没有要查找的“锚”,因此返回null

您只需将对
setContentView()
的调用从
onCreate
移动到
onResume
(假设您没有在
onCreate
中引用视图。对于正常使用,这与Android生命周期没有区别,
onResume
直接在
onCreate
之后和用户可以与活动交互之前调用

实际上,在某些应用程序中存在一个错误,您访问一个活动,然后启动另一个活动,可能将应用程序保留到另一个活动,然后返回,然后返回(通过“返回”按钮)到第一个活动。在这里,您突然看到一个黑屏。这是因为系统“已交换”退出第一个活动,返回时只调用
onResume
,但不调用
onCreate
,因此没有机会在那里再次加载布局。

执行所有这些((Button)findViewById(R.id.speakEnglishButton)).setText(“”;)。。。。 在onCreate()中

作为类变量,在onCreate()中初始化它们


在代码的后面部分,如果将setContentView()保留在onResume()而不是onCreate()中,则可以将其值修改为mspeakingButton.setWhather()

,它会出现充气异常,我已经尝试过了。

所有版面都有这个按钮吗?@PeterKnego只有一个版面,在res/layout/languageselection.XML中,这些按钮所在的版面XML文件在res/layout/mylayout.XML中,因此它是默认的权利?@Michael:找不到你可以解释一下吗more@MohitSharma盲人我想说的是,你应该在你的
layout
文件夹中放置一个
layout.xml
。你可能有
layout de
layout land
,但不是一个简单命名为
layout
,没有任何后缀的。如何解决这个问题?在onResume中设置内容视图,例如
setContentView(R.布局.语言选择)
?这在活动生命周期中是不可能的。它是否由某个Android发行票据解决?老实说,我不记得细节,因为这个答案是4年前的。我记得当时我确实遇到了这个问题,并且正在调试它。我不知道是否有票据。或者,在较新的Android版本中,这是否得到了修复ns.但是你有这个问题吗?。@MohitSharma我从来没有这样尝试过。我更喜欢引用我正在使用的控件,而不是在我需要使用任何控件时使用查找。但是为什么在onCreate回调之后出现onResume时他会出现这种情况呢?我注意到这个问题仍然发生在早期版本的Android上。它不应该重新出现在onResume中设置null,因为onResume在onCreate之后。但是,保持onCreate中的引用是一个很好的实践,findViewById会对视图进行线性查找,每次查找都会占用一点时间。
private Button mSpeakEngButton;
.....
public void onCreate() {

   ....
   mSpeakEngButton = ((Button)findViewById(R.id.speakEnglishButton));
}