java


Android Studio: Error of java.lang.NullPointerException when the headlessfragment called


I have a main Activity and a headless Fragment.
The headless Fragment is supposed to get the IMEI number of the phone to be recorded and returned to the main Activity.
I had this bug for a few hours now and I can't seem to shake it off.
Here's the logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androidproject.example, PID: 5418
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidproject.example/com.androidproject.example.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Activity.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Activity.getApplicationContext()' on a null object reference
at com.androidproject.example.HeadlessFragment.loadIMEI(HeadlessFragment.java:110)
at com.androidproject.example.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Application terminated.
And here's the relevant part of the code in the MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDeviceCode = (TextView)findViewById(R.id.device_code);
// Initializing headless fragment
mFragment =
(HeadlessFragment) getFragmentManager()
.findFragmentByTag("IMEILoader");
if(mFragment == null) {
mFragment = new HeadlessFragment();
getFragmentManager().beginTransaction()
.add(mFragment, "IMEILoader").commit();
}
if(mFragment != null){
mNumber = mFragment.loadIMEI(); //Here's the error
mDeviceCode.setText(Html.fromHtml("<b>IMEI</b>: " + mFragment.loadIMEI()));
}
And here's the HeadlessFragment code:
//Called when the 'loadIMEI' function is triggered.
public String loadIMEI() {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has not been granted.
requestPermissions();
} else {
// READ_PHONE_STATE permission is already been granted.
RecordedIMEI = permissionGrantedActions();
}
if(RecordedIMEI != null) {
Log.i("loadIMEIService", "IMEI number returned!");
}
return RecordedIMEI;
}
public String permissionGrantedActions() {
//Get IMEI Number of Phone
TelephonyManager tm =(TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
String IMEINumber = tm.getDeviceId();
//RecordedIMEI = IMEINumber;
if(IMEINumber != null) {
Log.i("IMEI Loader", "IMEI number recorded!");
}
return IMEINumber;
}
}
I tried different things but no luck. I think getActivity().getApplicationContext() is pointing no where, which means this is being called before headless fragment is attached to mainactivity?
I've been stuck on this for quite some time and need some help.
Change This Line
mNumber = mFragment.loadIMEI();
To
mNumber = mFragment.loadIMEI(MainActivity.this);
And your function would be like this.
public String loadIMEI(Context context) {
//Context context = getActivity().getApplicationContext();
//Activity activity = context instanceof Activity ? (Activity) context : null;
//mActivity = activity;
// Check if the READ_PHONE_STATE permission is already available.
//this.context = getActivity().getApplicationContext();
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has not been granted.
requestPermissions();
} else {
// READ_PHONE_STATE permission is already been granted.
RecordedIMEI = permissionGrantedActions();
}
if(RecordedIMEI != null) {
Log.i("loadIMEIService", "IMEI number returned!");
}
return RecordedIMEI;
}
I think you're going about this "headless Fragment" idea wrong since...
A Fragment represents a behavior or a portion of user interface in an Activity.
You seem to want a static utility class that holds a Context and can call some permission things. Take away the extends Fragment from the code and other answer(s), and this is basically what you are left with.
Turn it into a singleton, and you don't need the Context parameter everywhere.
(code untested)
May not completely work. For example, not sure how the ActivityCompat.OnRequestPermissionsResultCallback works...
But it exposes the functionality you need without working around a Fragment lifecycle.
public final class IMEILoader {
public static IMEILoader mInstance;
private Context mContext;
private IMEILoader() {}
private IMEILoader(Context c) {
this.mContext = c;
}
// Singleton pattern
public static IMEILoader getInstance(Context c) {
if (!(c instanceof ActivityCompat.OnRequestPermissionsResultCallback)) {
throw new Exception("Passed context not implementing permission callbacks");
}
if (mInstance == null) mInstance = new IMEILoader(c);
return mInstance;
}
public String load() {
String recordedIMEI = null;
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission has not been granted.
requestPermissions();
} else {
// READ_PHONE_STATE permission is already been granted.
recordedIMEI = permissionGrantedActions();
}
if(recordedIMEI != null) {
Log.i("loadIMEIService", "IMEI number returned!");
}
return recordedIMEI;
}
public String permissionGrantedActions() {
return null;
}
}
And you can use that in the Activity like
class FooActivity extends AppCompatActivity impelements ActivityCompat.OnRequestPermissionsResultCallback {
private IMEILoader loader;
...
public void onCreate(Bundle b) {
...
loader = IMEILoader.getInstance(MainActivity.this);
String blah = loader.load();
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
...
}

Related Links

NullPointerException onPostExecute() [duplicate]
Removing item from RecyclerView not working well
Disable X-FrameOptions response header for a URL Spring Security JAVA config
Heroku - Load balancing from web to background worker
Using personal library .jar in TFS Online build
How to remote debug a java process running on the play framework
Cant upload file through okhttp3
How to add alarms in Eclipse Milo
App crashes when running it on samsung j3
Axis2 Servlet : Etat HTTP 500 - “Servlet.init()” pour la servlet AxisServlet a généré une exception
GWTP Gatekeeper
Error: Could not find or load main class SnakeGame.Gameplay
JavaFX Scene Builder Refresh from Code
How to compare 3 ArrayLists and get the differences in 3 new ArrayLists?
Server access Error: Connection timed out
How to make sure that method is executed only once and from one thread only?

Categories

HOME
compiler-construction
wso2-am
osgi
wso2
keycloak
vmware
react-router
baqend
alpha
windows-server
adfs
ssl-client-authentication
spring-xd
row
nstableview
fallback
windows-server-2012
zebra-printers
quickfix
windows-7-x64
jsdoc
angular-ui
jprofiler
plunker
ml
errorlevel
web-sql
java-7
microsoft-sync-framework
brunch
traffic
phpfox
tooltipster
elasticsearch-ruby
libuv
c++-amp
iframe-resizer
key-value-observing
.net-4.0
titanium-mobile
kendo-ui-grid
pim
webtest
phpfreechat
wallpaper
amazon-kinesis-kpl
gulp-sourcemaps
grid.mvc
botbuilder
android-mediaprojection
mime
http-redirect
reactive-cocoa-5
galleria
wso2carbon
user-accounts
osx-mavericks
smartcontracts
chain-builder
jedis
domain-model
dropbox-php
return-value
fancybox-2
drupal-6
slickedit
django-scheduler
abcpdf9
gridpane
adxstudio-portals
transmitfile
nsfilemanager
clang-static-analyzer
actionbardrawertoggle
bluemix-app-scan
livequery
srand
xna-4.0
msys
mutation-observers
simple-framework
drawbitmap
microbenchmark
apache-commons-fileupload
createprocessasuser
cisco-ios
device-orientation
modalpopup
id3v2
article
tld
expected-exception
jboss-weld
typo3-neos
terminal-services
dir
opcache
ril
batterylevel
seed
android-screen-support
cassini-dev
tinn-r
dice
propertyeditor
dsn
buildr
netdna-api
adk
calling-convention
sudzc
gamequery
.nettiers
webkit.net
privilege
ugc

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App