您的位置:首页 > 移动开发 > Android开发

大四实习准备2_java异常处理_android控件练习

2015-04-24 23:20 337 查看
2015-4-24

Java 异常处理

可以有多个catch;ArrayIndexOutOfBoundsException类是Exception类的子类RuntimeException类的一个间接子类;finally{}一定被执行;

异常分类

1>继承关系

Object类->Throwable类->Error类(通常是硬件运行错误,通常不能通过程序来修改)(未捕获异常)、Exception类

Exception类->RuntimeException类(若不进行异常处理,可能编译时没问题,运行时就出错了)(未捕获异常)、其他类(捕获异常)。

2>

捕获异常(即 “必须处理异常”,通常由外部因素造成。可能出现该类异常而不try-catch 或者 不可能出现该类异常而try-catch,都会报错)。

未捕获异常

;

抛出异常(不立即处理异常,而是向上抛出异常,直到有地方处理为止)

public class test{
public static void main(String[] args) {
try{
test t = new test();
t.a();
}catch(Exception e){
//在这里处理异常
System.out.println("processd in main()");
}
}
private void a(){
b();
}
private void b(){
int[] r = new int[5];
r[5]=10;//在这里发生异常
}
}


或者 (?)使用throw、throws。

;

自定义异常

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/left">

<TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"
/>
</LinearLayout>

<LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/right">

<TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
/>
</LinearLayout>

</LinearLayout>


diy_left_right.xml 为ListView自定义的布局
问题:

MainActivity里

lv.setSelection(list.size());//将ListView定位到最后一行


觉得效果不对。(?)确实定位在新的数据那里了,但是原先的数据(除了第一条)都没了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: