提问者:小点点

无法创建类的实例MainViewModel_android


我尝试创建ViewModel的实例,但得到一个错误。

进程:com. example.人口电影,PID:20601 java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.人口电影/com.example.人口电影.MainActivity}:java.lang.RuntimeException:无法创建类com.example.popularmovies.data的实例。MainViewModel

主要活动:

public class MainActivity extends AppCompatActivity { 
    private MainViewModel viewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewModel = new ViewModelProvider(this).get(MainViewModel.class);

主视图模型

public class MainViewModel extends AndroidViewModel {

    public static MovieDatabase database;
    public final LiveData<List<Movie>> movies;

    public MainViewModel(@NonNull Application application) {
        super(application);
        database = MovieDatabase.getInstance(getApplication());
        movies = database.movieDao().getAllMovies();
    }

    public Movie getMovieById(int id) {
        try {
            return new GetMovieTask().execute(id).get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void deleteAllMovies() {
        new DeleteMoviesTask().execute();
    }

    public void insertMovie(Movie movie) {
        new InsertTask().execute(movie);
    }

    public void deleteMovie(Movie movie) {
        new DeleteTask().execute(movie);
    }

    public LiveData<List<Movie>> getMovies() {
        return movies;
    }

    public static class DeleteTask extends AsyncTask<Movie, Void, Void> {
        @Override
        protected Void doInBackground(Movie... movies) {
            if (movies != null && movies.length > 0) {
               database.movieDao().deleteMovie(movies[0]);
            }
            return null;
        }
    }

    public static class InsertTask extends AsyncTask<Movie, Void, Void>  {
        @Override
        protected Void doInBackground(Movie.. movies) {
            if (movies != null && movies.length > 0) {
                database.movieDao().insertMovie(movies[0]);
            }
            return null;
        }
    }

    public static class DeleteMoviesTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void.. integers) {
            database.movieDao().deleteAllMovies();
            return null;
        }
    }

    public static class GetMovieTask extends AsyncTask<Integer, Void, Movie> {
        @Override
        protected Movie doInBackground(Integer.. integers) {
            if (integers != null && integers.length > 0) {
                return database.movieDao().getMovieById(integers[0]);
            }
            return null;
        }
    }
}

数据库:

@Database(entities = {Movie.class}, version = 1, exportSchema = false)
public abstract class MovieDatabase extends RoomDatabase {

    private static final String DB_NAME = "movies.db";
    private static MovieDatabase database;
    private static final Object LOCK = new Object();

    public static MovieDatabase getInstance(Context context) {
        synchronized (LOCK) {
            if (database == null) {
                database = Room.databaseBuilder(context, MovieDatabase.class, DB_NAME).build();
            }
        }
        return database;
    }

    public abstract MovieDao movieDao();
}

共1个答案

匿名用户

也许这种错误只是因为将代码复制粘贴到这里,但您的视图模型中似乎存在语法问题。

您的类在构造函数之前结束。这似乎是完整的类:

public class MainViewModel extends AndroidViewModel {
    public static MovieDatabase database;
    public final LiveData<List<Movie>> movies;
}

只需删除},也许将其添加到类的末尾,尽管看起来不需要它(但是你的IDE应该抱怨这个语法问题?)