Reputation: 658
I/System.out: Failure -> Failed to invoke public androidx.lifecycle.LiveData() with no args
I am fetching data from the API. I am trying to use MVVM, i am new in MVVM. So i call the api from the ViewModel and i return the data in the method. I am observing that method from my MainActivity. But i don't know why is is giving me the follow error.
Retrofit goes in the failure callback. But it was going in the success callback when i was not using LiveData.
I/System.out: Failure -> Failed to invoke public androidx.lifecycle.LiveData() with no args
public class MainViewModel extends ViewModel {
private ApiInterface apiInterface;
private LiveData < List < Feature >> featuresList;
public MainViewModel() {
apiInterface = ApiClient.getClient().create(ApiInterface.class);
getEqData();
featuresList = new MutableLiveData < > ();
}
public void getEqData() {
apiInterface.getTodos().enqueue(new Callback < EarthquakeData > () {
@Override
public void onResponse(Call < EarthquakeData > call, Response < EarthquakeData > response) {
featuresList = response.body().features;
}
@Override
public void onFailure(Call < EarthquakeData > call, Throwable t) {
System.out.println("Failure -> " + t.getMessage());
}
});
}
public LiveData < List < Feature >> returnEqData() {
return featuresList;
}
}
public class MainActivity extends AppCompatActivity {
private static final String TAG = "sagar";
private MainViewModel mainViewModel;
private RecyclerView rvEarthquake;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvEarthquake = findViewById(R.id.rvEarthquake);
EarthquakeAdapter adapter = new EarthquakeAdapter(this);
rvEarthquake.setLayoutManager(new LinearLayoutManager(this));
rvEarthquake.setAdapter(adapter);
mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
mainViewModel.returnEqData().observe(this, new Observer < List < Feature >> () {
@Override
public void onChanged(List < Feature > features) {
adapter.setData(features);
}
});
}
}
Upvotes: 0
Views: 189
Reputation: 304
Use setValue or postValue to set value in featuresList like below
featuresList.setValue(response.body().features);
Upvotes: 2