安卓基础(AndroidViewModel)
今天学习了AndroidViewModel,但是我根据视频上讲解,根据所讲用例,在添加依赖得时候一直报错,后来我请教大佬,他告诉我说是,因为网络得问题,国外得一些依赖有可能下不下来,所以可以下载阿里云的依赖。
implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01'
需将build.gradle(Project)更改为以下:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
google()
//jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
google()
//jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
跟着视频所作的项目是永久保存数据,在正常重启情况下,也可以保存数据。
项目还是一个简单的加减运算,两个按钮,一个Textview。
MainActivity:
package com.example.androidviewmodel; import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import androidx.lifecycle.SavedStateVMFactory; import com.example.androidviewmodel.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity {
MyViewModel myViewModel;
ActivityMainBinding binding; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
myViewModel = ViewModelProviders.of(this, new SavedStateVMFactory(this)).get(MyViewModel.class);
binding.setLifecycleOwner(this);
binding.setData(myViewModel);
}
}
MyViewModel:
package com.example.androidviewmodel; import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull; import androidx.lifecycle.LiveData;
import androidx.lifecycle.SavedStateHandle; public class MyViewModel extends AndroidViewModel {
SavedStateHandle handle;
String key = getApplication().getResources().getString(R.string.key);
String mydata = getApplication().getResources().getString(R.string.MY_DATA); public MyViewModel(@NonNull Application application, SavedStateHandle handle) {
super(application);
this.handle = handle;
if (!handle.contains(key)) {
load();
}
} public LiveData<Integer> getnumber() {
return handle.getLiveData(key);
} void load() {
SharedPreferences shp = getApplication().getSharedPreferences(mydata, Context.MODE_PRIVATE);
int x = shp.getInt(key, 0);
handle.set(key, x);
} void save() {
SharedPreferences shp = getApplication().getSharedPreferences(mydata, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shp.edit();
editor.putInt(key, getnumber().getValue());
editor.apply();
} public void add(int x) {
handle.set(key, getnumber().getValue() + x);
}
}
但是我遇到了一个问题,我跟着视频所敲的代码,就是报错,一直找不出为什么,很难受。
Build显示:

Run显示:


一直说我无法创建Myviewmodel的实例,弄了大半天,还是没结果,渴望得到大佬的指点。
安卓基础(AndroidViewModel)的更多相关文章
- [安卓基础] 004.运行app
运行你的app 这篇课程会教你: 1.如何在设备上运行你的app. 2.如何在模拟器上运行你的app. 当然,在学习之前,你还需要知道: 1.如何使用设备. 2.如何使用模拟器. 3.管理你的项目. ...
- 安卓基础之Sqlite数据库最最基础操作
Sqlite数据库基础操作 摘要:在应用中新建一个数据库,并创建一个数据表写入数据,然后读取表中数据并展示. 主要逻辑: 1.通过继承SQLiteOpenHelper自定义类,定制数据库的表结构,初始 ...
- 【安卓基础】WebView开发优化基础
最近工作很忙,不仅要抽空进行管理,还有开发任务在身,幸好有一些规划进行指导,所以还能顺利解决问题.在管理和技术上面,我认为技术是硬实力,管理是软实力,自己需要多点时间花在技术上. 回归正题,在项目中的 ...
- [安卓基础] 006.打开另一个Activity
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- [安卓基础] 003.建立你的第一个App
创建一个android工程项目 我们使用android提供的集成开发工具(Eclipse+ADT)来创建android工程项目.用这个集成开发工具创建项目,简单,方便,快捷,且自动帮助我们生成基础的文 ...
- [安卓基础] 001.学习Android开发的好教程
如果想自学android,有许多不错的android网站.这里收集了一些,列举如下: 国内 极客学院,这里有非常丰富的视频教程. http://www.jikexueyuan.com/course/a ...
- [安卓基础]011存储数据(中)——sqlite语法介绍
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- [安卓基础] 007.管理Activity的生命周期
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 安卓基础之通过Intent跳转Activity
通过Intent跳转Activity 一.通过意图开启Activity的方式: 隐式意图:通过指定一组数据或者动作实现 Intent intent=new Intent(); intent.s ...
随机推荐
- Mike and strings
Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can ...
- Linux oracle安装 内核参数讲解
在安装Oracle的时候需要调整linux的内核参数,但是各参数代表什么含义呢,下面做详细解析. Linux安装文档中给出的最小值: fs.aio-max-nr = 1048576 fs.file-m ...
- (转)Hadoop Combiner
转自:http://blog.csdn.net/jokes000/article/details/7072963 众所周知,Hadoop框架使用Mapper将数据处理成一个<key,value& ...
- C#中获取时间戳
{ 注意:下面是以毫秒为单位的13位 UTC 时间戳(非正规) }//先取得当前的UTC时间,然后转换成计算用的周期数(简称计时周期数),每个周期为100纳钞(ns)=0.1微秒(us)=0.00 ...
- HTML连载61-焦点图、固定定位
一.焦点图 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- crowdfunding项目03——mapper映射错误
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'TAd ...
- 19年SD夏令营游记
首先,因为自己的刻苦学习(tui),所以游记很短,勿喷... 7.22.2019——报到 话说昨晚热到12点才睡着,在路上大家一直都在玩游戏,没有游戏可玩的我听着歌发呆... 到了山东省外国语职业技术 ...
- bistoury建库建表(一)
bistoury DROP TABLE IF EXISTS bistoury_app; CREATE TABLE bistoury_app( id INT UNSIGNED auto_incremen ...
- 题解 P1453 【城市环路】
P1453 城市环路 感觉基环树(or环套树)的题目一般都是找到树上的环,断掉一条边再进行树上的操作(如noip2018P5022 旅行) 双倍经验:P2607 [ZJOI2008]骑士 P1453和 ...
- Java学习笔记(九)面向对象---模板方法设计模式
理解 在定义功能时功能的一部分是确定的,但是有一部分是不确定的,而确定的部分在使用不确定的部分,那么就将不确定的部分暴露出去,由该类的子类完成. 举例 需求 获取一段程序的运行时间 代码 abstra ...