Activity生命周期
Fragment的生命周期
Activity 獲取 view getContentView
public static View getContentView(Activity ac){
ViewGroup view = (ViewGroup)ac.getWindow().getDecorView();
FrameLayout content = (FrameLayout)view.findViewById(android.R.id.content);
return content.getChildAt(0);
}
獲取GroupView的子控件及個(gè)數(shù)
int childCount=ll.getChildCount(); //獲取groudview有幾個(gè)子view
ll.getChildAt(0); //獲取某個(gè)為子view
# 代碼動(dòng)態(tài)添加控件
public void onClickbtnAddbtn(View view) {
Button btn=new Button(this);
btn.setText("刪除我");
myConainer.addView(btn);
//設(shè)置動(dòng)畫
RotateAnimation ra=new RotateAnimation(0,360,100,150);//從0到360讀旋轉(zhuǎn)100,50的中心的像素為中心點(diǎn)選擇
ra.setDuration(1000);
LayoutAnimationController lac=new LayoutAnimationController(ra,0.2f); //第二個(gè)參數(shù)是動(dòng)畫運(yùn)行到百分多少時(shí)诬垂,第二個(gè)對(duì)象開始動(dòng)畫
lac.setOrder(LayoutAnimationController.ORDER_RANDOM); //設(shè)置動(dòng)畫效果的順序LayoutAnimationController下提供隨機(jī)、從上往下俺猿、從下往上
myConainer.setLayoutAnimation(lac);
//btn.setOnClickListener(btn_OnClickListener);
}
其他
LayoutParams param1 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
param1.addRule(RelativeLayout.BELOW, 1);//此控件在id為1的控件的下邊
rl.addView(tv,param1);
Button update = new Button(this);
update.setText(Button);
LayoutParams param2 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
param2.addRule(RelativeLayout.RIGHT_OF, 1);//此控件在id為1的控件的右邊
rl.addView(update,param2);
this.addView(rl, relLayoutParams);
更多參考:http://blog.csdn.net/u012881904/article/details/48394169
Error:This Gradle plugin requires Studio 3.0 minimum
是我的studio的版本不夠高焕盟,我的studio是2.3.3版本的查坪,導(dǎo)入的項(xiàng)目的版本應(yīng)該是3.0的,其實(shí)我們可以這樣,將以下內(nèi)容添加到gradle.properties中:android.injected.build.model.only.versioned = 3
android.injected.build.model.only.versioned = 3
問題:
Failure [INSTALL_FAILED_TEST_ONLY]
java.lang.Exception: exit callstack! code=1
at java.lang.System.exit(System.java:636)
at com.android.commands.pm.Pm.main(Pm.java:109)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:249)
解決辦法:
這個(gè)我問題是由于gradle plugin, gradle tool是版本問題台谢,版本配置不對(duì)導(dǎo)致的姓言,重新配置下工程的gradle版本 瞬项,比如我用的是AS2.3.2
gradle配置如下:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// classpath 'com.android.tools.build:gradle:2.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
Error:(71, 0) Could not find method implementation() for arguments [directory 'libs'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
解決辦法
build.gradle 文件中用compile
替換implementation
compile
最近被棄用
,取而代之的是implementation
或api
dependencies {
...
// implementation fileTree(include: ['*.jar'], dir: 'libs')
compile fileTree(include: ['*.jar'], dir: 'libs')
// implementation "com.android.support:appcompat-v7:${SUPPORT_LIBRARY_VERSION}"
compile "com.android.support:appcompat-v7:${SUPPORT_LIBRARY_VERSION}"
...
}
Error:(119, 0) Could not find method debugImplementation() for arguments
參見:### Android Studio3.0新的依賴方式(implementation何荚、api滥壕、compile...
問題
distributionUrl=https://services.gradle.org/distributions/gradle-3.3-all.zip
Android中保存Activity狀態(tài)
在android中,一個(gè)界面處于不可見的狀態(tài)的時(shí)候兽泣,當(dāng)系統(tǒng)的內(nèi)存不足的時(shí)候绎橘,就有可能會(huì)被 系統(tǒng)kill到,那么唠倦,當(dāng)被kill的時(shí)候称鳞,會(huì)調(diào)用方法onSaveInstanceState,
那么我們可以在這里面保存一些數(shù)據(jù)
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
// etc.
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}
當(dāng)保存了相關(guān)狀態(tài)后稠鼻,我們也可以在onCreate()方法中進(jìn)行數(shù)據(jù)的恢復(fù)
重新恢復(fù)數(shù)據(jù)
if (savedInstanceState != null) {
}
當(dāng)然也可以先將相關(guān)數(shù)據(jù)保存到本地目錄 冈止,然后在activity重建的時(shí)候 ,在oncreate()方法中進(jìn)行數(shù)據(jù)的重新獲取
Activity按返回鍵2次退出app
private long exitTime = 0;
@Override
public void onBackPressed() {
long currentTime = System.currentTimeMillis();
if ((currentTime - exitTime) < 2000) {
super.onBackPressed();
} else {
Toast.makeText(this, R.string.double_click_exit, Toast.LENGTH_SHORT).show();
exitTime = currentTime;
}
}
java 為接口創(chuàng)建實(shí)例--參考RxJava中的Observer 接口
//接口定義
public interface Observer<T> {
void onSubscribe(@NonNull Disposable d);
void onNext(@NonNull T t);
void onError(@NonNull Throwable e);
void onComplete();
}
//創(chuàng)建實(shí)例
Observer<String> observer = new Observer<String>() {
@Override
public void onNext(String s) {
Log.i("onNext ---> ", "Item: " + s);
}
@Override
public void onCompleted() {
Log.i("onCompleted ---> ", "完成");
}
@Override
public void onError(Throwable e) {
Log.i("onError ---> ", e.toString());
}
};
問題
運(yùn)行環(huán)境android5.1
錯(cuò)誤信息
12-28 14:46:25.373: A/libc(20321): Fatal signal 11 (SIGSEGV), code 2, fault addr 0xb74199c8 in tid 20321 (package)
sqlcipher 庫的System.loadLibrary("database_sqlcipher") 發(fā)生奔潰
其實(shí)就是net.sqlcipher.database.SQLiteDatabase.loadLibs(context) 發(fā)生錯(cuò)誤
Android5.0上運(yùn)行SQLCipher庫出現(xiàn)readonly database的解決方案
文章提到:“這個(gè)問題只有在Android5.0上才會(huì)出現(xiàn)候齿,但是目前采用5.0版本的機(jī)型應(yīng)該不會(huì)”熙暴,由于我的用戶沒有在android5.0上闺属,所以我就沒有按文章的方法去解決。用5.1嘛起碼穩(wěn)定點(diǎn)
問題:使用sqlcipher庫時(shí)周霉,在SQLiteDatabase.loadLibs(context);位置發(fā)生奔潰
解決:
把libs中其他多余的so庫刪除后掂器,得到解決。