AppCompatActivity未观察到ViewModel

时间:2018-12-12 09:35:19

标签: java android gradle viewmodel android-architecture-components

我正在尝试在EarthquakeActivity中一起使用ViewModel,但是我一直收到此错误:

Wrong 1st argument type. Found: 'com.example.android.quakereport.EarthquakeActivity', required: 'androidx.lifecycle.LifecycleOwner'

这是我尝试做的时候

mainViewModel.getEarthquakeData().observe(this, new Observer<List<Earthquake>>() {
        @Override
        public void onChanged(List<Earthquake> earthquakes) {
                // do something
        }
    });

活动this返回错误。

我读到hereAppCompatActivity已经扩展了LifecycleOwner,这正是我所使用的。怎么了其他所有教程都没有这个问题。

我的EarthquakeActivity.java

package com.example.android.quakereport;

import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import POJO.Earthquake;
import POJO.RootEarthquakeResponse;
import androidx.lifecycle.Observer;
import retrofit2.Call;


public class EarthquakeActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private LinearLayoutManager  layoutManager;

    public static final String LOG_TAG = EarthquakeActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.earthquake_activity);

        // Create a fake list of earthquake locations.
        final ArrayList<Earthquake> earthquakes = getEarthquakes();

        setupRecyclerView(earthquakes); // set up recycler view

        MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
        mainViewModel.getEarthquakeData().observe(this, new Observer<List<Earthquake>>() {
            @Override
            public void onChanged(List<Earthquake> earthquakes) {
                    // do something
            }
        });

    }

    private void setupRecyclerView(ArrayList<Earthquake> earthquakes){
        // set up recyclerview
    }
}

我的MainViewModel:

package com.example.android.quakereport;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import POJO.Earthquake;
import POJO.Feature;
import POJO.RootEarthquakeResponse;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainViewModel extends AndroidViewModel {

    private MutableLiveData<List<Earthquake>> earthquakeLiveData;
    private String LOG_TAG = MainViewModel.class.getName();

    public MainViewModel(@NonNull Application application) {
        super(application);
//        earthquakeLiveData = new EarthquakeLiveData();
    }

    public LiveData<List<Earthquake>> getEarthquakeData(){
        if (earthquakeLiveData == null){
            earthquakeLiveData = new MutableLiveData<>();
            loadEarthquakes();
        }

        return earthquakeLiveData;
    }

    private void loadEarthquakes(){
        EarthquakeService earthquakeService = EarthquakeServiceGenerator.createService(EarthquakeService.class);

        Call<RootEarthquakeResponse> call = earthquakeService.getEarthquakes("geojson", "time", 6, 10);

        call.enqueue(new Callback<RootEarthquakeResponse>() {
            @Override
            public void onResponse(Call<RootEarthquakeResponse> call, Response<RootEarthquakeResponse> response) {
                List<Earthquake> earthquakes = new ArrayList<>();
                for (Feature f: response.body().getFeatures()) {
                    earthquakes.add(f.getEarthquake());
                }

                earthquakeLiveData.setValue(earthquakes);
                Log.d(LOG_TAG, "Successful!");
                Log.d(LOG_TAG, response.body().toString());
            }

            @Override
            public void onFailure(Call<RootEarthquakeResponse> call, Throwable t) {
                Log.e(LOG_TAG, call.request().toString()); // check request if failed
                Log.e(LOG_TAG, t.toString()); // check error
            }
        });
    }
}

这是我的gradle依赖项:

implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'

implementation "androidx.lifecycle:lifecycle-viewmodel:2.0.0" // use -ktx for Kotlin
// alternatively - just LiveData
implementation "androidx.lifecycle:lifecycle-livedata:2.0.0"
implementation "android.arch.lifecycle:extensions:1.1.1"

我是否需要处理最重要的生命周期,或者我的依赖项是否有问题?

2 个答案:

答案 0 :(得分:6)

您正在使用AndroidX库中的df<-iris[1:20,] ,还使用了nr <- nrow(df) windowSize <- 5 lapply(seq_len(nr-windowSize+1), function(i) df[i:(i+windowSize-1),]) 的支持库。

将您的支持库迁移到AndroidX,以解决您的问题。

答案 1 :(得分:0)

从AndroidX库导入AppCompatActivity:

import androidx.appcompat.app.AppCompatActivity