简体   繁体   中英

Does getApplicationContext throw exception?

I tried reading the Android documentation but there doesn't seem to be any information on how to handle cases where getApplicationContext() returns null.

Should i do a check with a

try{
    mContext = application.getApplicationContext()
} catch (NullPointerException e) 

}

or should i do a null check

if(application.getApplicationContext() == null)
{
 return;
}

Does getApplicationContext throw any Exception or Fatal Exception? Or just returns null?

I tried reading the Android documentation but there doesn't seem to be any information on how to handle cases where getApplicationContext() returns null.

There is nothing in the documentation to suggest that it ever returns null.

Should i do a check with a

try{
    mContext = application.getApplicationContext()
} catch (NullPointerException e) 

}

No, why? The only way that can ever catch anything is if application is null, according to the Javadoc.

or should i do a null check

if(application.getApplicationContext() == null)
{
 return;
}

I don't see why. There is nothing to suggest that it ever returns null.

Does getApplicationContext throw any Exception or Fatal Exception? Or just returns null?

According to the Javadoc, neither. Of course if the Javadoc is wrong it's anybody's guess, but I can't see how the application context can ever be null in a running activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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