尝试在空对象引用上调用虚拟方法'.MutableLiveData .getLiveData(java.lang.String)'



我正在尝试实现ViewModel模块,但它给了我这个空指针异常错误。

这是错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ssnwa.cargoin.cargoincustomer, PID: 11374
java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.lifecycle.MutableLiveData androidx.lifecycle.SavedStateHandle.getLiveData(java.lang.String)' on a null object reference
at com.ssnwa.cargoin.cargoincustomer.DriverToPickupViewModel.getPickupLocation(DriverToPickupViewModel.java:43)
at com.ssnwa.cargoin.cargoincustomer.DrivertoPickupActivity.onMapReady(DrivertoPickupActivity.java:287)
at com.google.android.gms.maps.zzak.zza(Unknown Source:2)
at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source:12)
at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source:12)
at android.os.Binder.transact(Binder.java:627)
at cj.b(:com.google.android.gms.dynamite_mapsdynamite@19831052@19.8.31 (040700-0):14)
at com.google.android.gms.maps.internal.bb.a(:com.google.android.gms.dynamite_mapsdynamite@19831052@19.8.31 (040700-0):4)
at com.google.maps.api.android.lib6.impl.bi.run(:com.google.android.gms.dynamite_mapsdynamite@19831052@19.8.31 (040700-0):2)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

以下是调用活动:

public class DrivertoPickupActivity extends AppCompatActivity implements
OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener , RoutingListener {
public static String driverphno;
public Marker drivermarker;
public static Location driverloc=new Location("");
public static Location pickuploc=new Location("");
private DriverToPickupViewModel vm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.driver_reach_pickup_activity);
vm = new ViewModelProvider(this).get(DriverToPickupViewModel.class);
mpleasewait=new ProgressDialog(this);
mpleasewait.setTitle("Please Wait ...");

pickuploc.setLatitude(pickcordinates.latitude);
pickuploc.setLongitude(pickcordinates.longitude);


database=FirebaseDatabase.getInstance();
d2=FirebaseDatabase.getInstance();
ref=database.getReference();
ref2=d2.getReference();
drivername=findViewById(R.id.driver_name);
number=findViewById(R.id.phone_number);
kmtoreach=findViewById(R.id.reach_km_to_pickup);
timetoreach=findViewById(R.id.reach_time_to_pickup);
cancelbooking=findViewById(R.id.cancel_booking);
inforide=findViewById(R.id.info_ride);


SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.drivermap);
mapFragment.getMapAsync(this);

setupActions();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);

if(vm!=null){
//CRASHES AT THIS LINE AND ALL OTHER SIMILAR LINES THAT FOLLOW
vm.getdrivername().observe(this, new Observer<String>() {
@Override
public void onChanged(String s) {
drivername.setText(s);
}
});
vm.getdriverphone().observe(this, new Observer<String>() {
@Override
public void onChanged(String s) {
number.setText(s);
}
});
vm.getdistance().observe(this, new Observer<String>() {
@Override
public void onChanged(String s) {
kmtoreach.setText(s);
}
});
vm.gettime().observe(this, new Observer<String>() {
@Override
public void onChanged(String s) {
timetoreach.setText(s);
}
});
}

}

它在此行和随后的所有其他类似行崩溃

vm.getdrivername()

这是视图模型类

public class DriverToPickupViewModel extends ViewModel {
private SavedStateHandle mState;
public DriverToPickupViewModel(){}
public DriverToPickupViewModel(SavedStateHandle savedStateHandle){
mState = savedStateHandle;
}
void setPickupLocation(Location L){
mState.set("Pickup",L);
}
void setDriverLocation(Location L){
mState.set("Driver",L);
}
void setdrivername(String drivername){
mState.set("drivername",drivername);
}
void setdriverphone(String phone){
mState.set("phone", phone);
}
void setdistance(String distance){
mState.set("distance",distance);
}
void settime(String time){
mState.set("time",time);
}
LiveData<Location> getPickupLocation(){
return mState.getLiveData("Pickup");
}
LiveData<Location> getDriverLocation(){
return mState.getLiveData("Driver");
}
LiveData<String> getdrivername(){
return mState.getLiveData("drivername");
}
LiveData<String> getdriverphone(){
return mState.getLiveData("phone");
}
LiveData<String> getdistance(){
return mState.getLiveData("distance");
}
LiveData<String> gettime(){
return mState.getLiveData("time");
}
}

我无法理解为什么会发生这种情况。 我已经检查了vm==null.它在这里没有崩溃,这意味着视图模型不为空,但它在每个vm.get****方法都崩溃

根据最近的答案,mState 为空,这对我来说是愚蠢的,看不到它。

但我试图将其更正为:

vm = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(),this)).get(DriverToPickupViewModel.class);

我收到错误:

Activity cannot be convert to SavedStateRegistry owner
vm

不是空的,但mState是。空构造函数不设置mState,因此为空。 然后,getdrivername尝试在 null 引用上调用方法。

相关内容

  • 没有找到相关文章

最新更新