小编Rub*_*ben的帖子

AndroidViewModel 没有零参数构造函数

我正在尝试在 MainActivity 中创建我的 AndroidViewModel 实例。当我这样做时,我得到以下错误没有零参数构造函数

这是我的 RecipeViewModel

package com.example.kookrecepten;

import android.app.Application;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import java.util.List;

public class RecipeViewModel extends AndroidViewModel {
    private RecipeRepository recipeRepository;
    private LiveData<List<Recipe>> allRecipes;

    public RecipeViewModel(Application application) {
        super(application);
        recipeRepository = new RecipeRepository(application);
        allRecipes = recipeRepository.getAllRecipes();
    }

    public void insert(Recipe recipe) {
        recipeRepository.insert(recipe);
    }

    public void update(Recipe recipe) {
        recipeRepository.update(recipe);
    }

    public void delete(Recipe recipe) {
        recipeRepository.delete(recipe);
    }

    public void deleteAll() {
        recipeRepository.deleteAllRecipes();
    }

    public LiveData<List<Recipe>> getAllRecipes() {
        return allRecipes;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我错了,现在纠正我,但 AndroidViewModel …

java android android-lifecycle android-studio android-viewmodel

12
推荐指数
2
解决办法
8170
查看次数

Laravel:将图像保存在数据库中

我正在尝试在我的图像表中存储与文章表相关的图像

当我这样做时,会出现以下错误:

间接修改重载属性 App\Article::$thumbnail 没有效果

我的文章模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $fillable = [
        'title', 'exerpt', 'body'
    ];

    public function author()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }

    public function thumbnail()
    {
        return $this->hasOne(Image::class);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的图像模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Image extends Model
{
    public function article()
    {
        return $this->belongsTo(Article::class);
    }
}
Run Code Online (Sandbox Code Playgroud)

以及我的 ArticleController 中的 store 方法:

public function store(Request $request)
    {
        $article = new …
Run Code Online (Sandbox Code Playgroud)

php database laravel

5
推荐指数
1
解决办法
4338
查看次数