在我的登錄和註銷功能的應用程序中,我正在檢查活動啓動時共享首選項是否爲空,但我仍然收到NullPointerException。如何在android中檢查共享偏好是否爲null?
這裏是我的HomeActivity.java代碼:
// Username And Password Editbox
String eusername;
String epassword;
// Shared Preferences String declaration
String spusername;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
spusername=spreferences.getString("spusername","");
if(!spusername.equals(""))
{
Log.e("Spusername not null","-->"+spusername);
}
if(spusername==null)
{
Log.e("Spusername null","-->"+spusername);
}
}
您可以請分享您的例外日誌詳細信息 – swiftBoy
哪個語句拋出空指針異常? –
*檢查共享首選項是否爲null *是否指變量'spreferences'爲空? – t0mm13b