Activity-生命周期和程序测试
1、Android程序错误以及程序调试
Android程序设计过程中会遇到2大类错误问题,一类是运行前语法错误,一类是运行时错误。
运行前错误是比较明显的,这类错误不修改,apk不会生成也就不能运行。这类错误需要从Problems和Console视窗中找。这类错误常见的有资源文件名命名错误(必须是a-z0-9_.),R资源包找不到,某个组件的属性缺少或者是属性名错误、属性值错误。
运行时错误比较麻烦,没有语法错误,运行时才会发生错误,这需要我们利用LogCat日志视窗来找错误。常见的错误一般有2种情况,一种是源程序中没找对象去访问它,这种也包括虽然找了但是找的对象不在本页面上也会发生此类错误;再一种错误是单独写Activity文件和Layout布局文件后忘记在AndroidManifest.xml文件中注册新写的Activity。对于这类错误如果用LogCat日志也发现不了的时候,就需要断点调试,逐语句去调试,这是终极解决办法。
2、Activity的生命周期
我们可以通过Log.d()监测Activity的从开始到销毁的整个过程。
3.演示案例
方法调用:右键单击,👉ource 👉overrid/implement methods
`main xml文件`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hhh"
android:id="@+id/hhh"
/>
</LinearLayout>
`main Activity`
package com.example.myfirst;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.content.Intent;
public class MainActivity extends Activity {
public static final String TAG="MyFirst";
private Button hhh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "MainActivity onCreate");
//找对象
hhh =(Button)findViewById(R.id.hhh);
hhh.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,XianXingActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d(TAG, "MainActivity onStart");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d(TAG, "MainActivity onRestart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(TAG, "MainActivity onResume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(TAG, "MainActivity onPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(TAG, "MainActivity onStop");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(TAG, "MainActivity onDestroy");
}
}
`Xianxing.XML`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
`Xianxing Activity`
package com.example.myfirst;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class XianXingActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xian_xing);
Log.d(MainActivity.TAG, "XianXingActivity onCreate");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d(MainActivity.TAG, "XianXingActivity onStart");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d(MainActivity.TAG, "XianXingActivity onRestart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(MainActivity.TAG, "XianXingActivity onResume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(MainActivity.TAG, "XianXingActivity onPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(MainActivity.TAG, "XianXingActivity onStop");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(MainActivity.TAG, "XianXingActivity onDestroy");
}
}
效果 ,在log里看
新建监听log
点击绿色加号,