Android Studio: Error Of Java.lang.nullpointerexception When The Headlessfragment Called
Solution 1:
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 classIMEILoader {
publicstaticIMEILoader mInstance;
privateContext mContext;
privateIMEILoader() {}
privateIMEILoader(Context c) {
this.mContext = c;
}
// Singleton patternpublicstaticIMEILoadergetInstance(Context c) {
if (!(c instanceofActivityCompat.OnRequestPermissionsResultCallback)) {
thrownewException("Passed context not implementing permission callbacks");
}
if (mInstance == null) mInstance = newIMEILoader(c);
return mInstance;
}
publicStringload() {
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;
}
publicStringpermissionGrantedActions() {
returnnull;
}
}
And you can use that in the Activity like
classFooActivityextendsAppCompatActivity impelements ActivityCompat.OnRequestPermissionsResultCallback {
private IMEILoader loader;
...
publicvoidonCreate(Bundle b) {
...
loader = IMEILoader.getInstance(MainActivity.this);
Stringblah= loader.load();
}
@OverridepublicvoidonRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
...
}
Solution 2:
Change This Line
mNumber = mFragment.loadIMEI();
To
mNumber = mFragment.loadIMEI(MainActivity.this);
And your function would be like this.
publicStringloadIMEI(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!");
}
returnRecordedIMEI;
}
Post a Comment for "Android Studio: Error Of Java.lang.nullpointerexception When The Headlessfragment Called"