I am working on an app in which I have one Activity
that initially loads a Fragment
. I am getting IMEI No, Latitude, Longitude in the activity and I pass that data into fragment and also get that data in fragment but I am getting null pointer exception when I get data.
Initially My Account Activity loads Sign Up Fragment and also I am sending data from Account Activity to Sign Up Fragment
// Activity Code to send data from
// setting SignUp Fragment in Account Activity
changeFragments(new SignupFragment());
// Now call function GeoLocation and send it to Sign Up Fragment
public void GeoLocation() {
Bundle bundle = new Bundle();
bundle.putString("imei_no", imei_number);
bundle.putString("lat",latitude.toString());
bundle.putString("long",longitude.toString());
bundle.putString("postal_code",postalCode);
SignupFragment fragObj = new SignupFragment();
fragObj.setArguments(bundle);
}
// Now Sign Up Fragment > I am getting these fields where I call Api
private void callApi() {
String imei_no = getArguments().getString("imei_no");
String latitude = getArguments().getString("lat");
String longitude = getArguments().getString("long");
String postal_code = getArguments().getString("postal_code");
}
I am getting Null Pointer exception here.
changeFragments(fragobj);
更改这一行,因为如果您当时使用新的 SingupFragment 它将创建新对象
You should pass the instance of fragment instead of creating new one like:-
public void GeoLocation() {
Bundle bundle = new Bundle();
bundle.putString("imei_no", imei_number);
bundle.putString("lat",latitude.toString());
bundle.putString("long",longitude.toString());
bundle.putString("postal_code",postalCode);
SignupFragment fragobj = new SignupFragment();
fragobj.setArguments(bundle);
changeFragments(fragobj);
}
here I call the changeFragments() method inside GeoLocation.
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.