Repository MVVM体系结构中的java错误



我想在我的应用程序中使用Room db来实现MVVM。我关注了这个链接https://codelabs.developers.google.com/codelabs/android-room-with-a-view/index.html?index=..%2F..index#13

我在创建视图模型的实例时出错。当我解决了这个问题时,我最终出现了以下新错误:

Process: com.example.dailyfaithapp, PID: 7341
java.lang.StackOverflowError: stack size 8192KB
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
2020-04-28 18:10:23.865 7341-7341/com.example.dailyfaithapp E/AndroidRuntime:     at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
2020-04-28 18:10:29.491 7341-7356/com.example.dailyfaithapp I/e.dailyfaithap: Background concurrent copying GC freed 395507(38MB) AllocSpace objects, 1(3804KB) LOS objects, 5% free, 95MB/101MB, paused 337us total 142.457ms
2020-04-28 18:10:29.720 7341-7341/com.example.dailyfaithapp I/chatty: uid=10141(com.example.dailyfaithapp) identical 8100 lines
2020-04-28 18:10:29.720 7341-7341/com.example.dailyfaithapp E/AndroidRuntime:     at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
2020-04-28 18:10:29.720 7341-7341/com.example.dailyfaithapp E/AndroidRuntime:     at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.Repositories.FavouriteRepository.getAllFavourites(FavouriteRepository.java:25)
at com.example.dailyfaithapp.ViewModels.FavouritesViewModel.<init>(FavouritesViewModel.java:26)
at com.example.dailyfaithapp.FavouritesViewModelFactory.create(FavouritesViewModelFactory.java:25)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.example.dailyfaithapp.Activities.FavouritesActivity.setUpUI(FavouritesActivity.java:72)
at com.example.dailyfaithapp.Activities.FavouritesActivity.onCreate(FavouritesActivity.java:54)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

我试图增加项目的堆大小,这是我可以增加的最大值

org.gradle.jvmargs=-Xmx2048M -Dkotlin.daemon.jvm.options="-Xmx2048M"

这是我的视图模型

public class FavouritesViewModel extends AndroidViewModel {
public FavouriteRepository mRepository;
public LiveData<List<Favourites>> mAllFavourites;
private MutableLiveData<List<Favourites>> favouriteSearchResults;
public FavouritesViewModel (Application application) {
super(application);
mRepository = new FavouriteRepository(application);
mAllFavourites = mRepository.getAllFavourites();
favouriteSearchResults = mRepository.getFavouritesSearch();
}
public LiveData<List<Favourites>> getmAllFavourites() { return mAllFavourites; }
}

回购

public class FavouriteRepository {
private MutableLiveData<List<Favourites>> favouritesSearch =
new MutableLiveData<>();
private LiveData<List<Favourites>> allFavourites;
public LiveData<List<Favourites>> getAllFavourites() {
return getAllFavourites();
}
public MutableLiveData<List<Favourites>> getFavouritesSearch() {
return getFavouritesSearch();
}
}

查看模型工厂

public class FavouritesViewModelFactory implements ViewModelProvider.Factory {
private Application mApplication;
private String mParam;

public FavouritesViewModelFactory(Application application) {
mApplication = application;
}

@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
return (T) new FavouritesViewModel(mApplication);
}
}

数据库

@Database(entities = {Favourites.class}, version = 1, exportSchema = false)
public  abstract class FavouritesDatabase extends RoomDatabase {
public abstract FavouritesDAO favouritesDAO();
private static FavouritesDatabase INSTANCE;
public static FavouritesDatabase getFavouritesDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (FavouritesDatabase.class) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(
context.getApplicationContext(),
FavouritesDatabase.class,
"favourites_database"
).build();
}
}
}
return INSTANCE;
}
}

DAO

@Dao
public interface FavouritesDAO {
@Query("SELECT * FROM favourites")
LiveData<List<Favourites>> getAllFavourites();
@Query("SELECT * FROM favourites")
List<Favourites> loadAllFavorites();
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAllFavourites(Favourites... favourites);
@Insert(onConflict = OnConflictStrategy.REPLACE)
public void insertFavourite(Favourites favourites);
@Delete
void delete(Favourites favourites);
}

在调试过程中,我看到repository方法的getAllFavorites((被称为continuous,因此内存越来越满。

这是我的活动,

public void setUpUI(){
recyclerView = findViewById(R.id.recycler_view_favourites);
constraintLayoutEmpty = findViewById(R.id.constraint_empty);
imageViewBack = findViewById(R.id.imageView_back);
imageViewBack.setOnClickListener(this);

favouritesViewModel = new ViewModelProvider(this,
new FavouritesViewModelFactory(getApplication())).get(FavouritesViewModel.class);

favouritesViewModel.getmAllFavourites().observe(this,
new Observer<List<Favourites>>() {
@Override
public void onChanged(@Nullable final List<Favourites> products) {
favouritesArrayList = products;
}
});
}

在活动的onCreate上调用setUpUI。

请帮忙。非常感谢。

FavouriteRepository中,您正在执行:

public LiveData<List<Favourites>> getAllFavourites() {
return getAllFavourites();
}

这个方法是在调用自己吗。你必须看到你需要什么才能到达这里,而不是自称。

我想你在下一个方法中也有同样的问题:

public MutableLiveData<List<Favourites>> getFavouritesSearch() {
return getFavouritesSearch();
}

你需要修复这些。

相关内容

  • 没有找到相关文章

最新更新