我从我的一些用户那里得到了一个 NullPointerException ,我无法发现问题是什么,它被抛出在这条线上:
((Button)findViewById(R.id.speakEnglishButton)).setText("");
我看不出有什么问题,该按钮与该 ID 存在,它编译正常,在我的模拟器和 2 个设备上工作正常,但是我在我的开发人员控制台中为用户发布了大约 100 个错误版本。
仔细查看 onResume :
@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));
}
}
本质上,我所做的只是检查保存的首选项布尔值,并根据它的值在按钮上设置标签,或者将它们设置为空白。
有人可以请教吗?
谢谢