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。
;
自定义异常
//继承自Exception,写了两个构造函数。可以参考Exception类的代码
class myException extends Exception{
public myException() {
// TODO Auto-generated constructor stub
}
public myException(String s){
super(s);
}
} public class test{
//定义一个可能发生自定义异常的方法
public String yiChang(int x) throws myException{
if( x>0 ){
return "Right";
}
else{
throw new myException("Negavite");
}
}
public static void main(String[] args){
test t1 = new test(); try{
System.out.println(t1.yiChang(5));
}catch(myException e){
System.out.println("异常信息为:"+e.getMessage());
} try{
System.out.println(t1.yiChang(-2));
}catch(myException e){
System.out.println("异常信息为:"+e.getMessage());
//关注这些方法e.getMessage(),e.toString(),e.printStackTrace()
}
}
}
自定义异常示例
android 控件练习UIBestPractice
package com.example.uibestpractice; import java.util.ArrayList; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity {
ArrayList<talk> list = new ArrayList<talk>();
ListView lv;
EditText et;
Button bt;
talkAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inittalk();
adapter = new talkAdapter(MainActivity.this, R.layout.diy_left_right, list);
lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(adapter);
et = (EditText) findViewById(R.id.edittext);
bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new OnClickListener() { @Override
public void onClick(View view) {
// TODO Auto-generated method stub
String content = et.getText().toString();
if( !content.equals("") ){
talk t = new talk(content,talk.TYPE_SEND);
list.add(t);
adapter.notifyDataSetChanged();//当有新消息是,刷新ListView的显示
lv.setSelection(list.size());//将ListView定位到最后一行
et.setText(""+list.size());//清空输入框中的内容
}
}
});
}
private void inittalk() {
//TODO Auto-generated method stub
talk t = new talk("hello kiwi",talk.TYPE_RECEIVED);
list.add(t);
t = new talk("hello tryonce",talk.TYPE_SEND);
list.add(t);
t = new talk("Come on together",talk.TYPE_RECEIVED);
list.add(t);
t = new talk("It's my honor,my dearling",talk.TYPE_SEND);
list.add(t);
}
}
MainActivity.java
package com.example.uibestpractice; public class talk {
public static final int TYPE_RECEIVED = 0;
public static final int TYPE_SEND = 1;
private String s;
private int type;
public talk(String s,int type){
this.s = s;
this.type = type;
}
public String getContent(){
return s;
}
public int getType(){
return type;
}
}
talk.java ListView的数据类
package com.example.uibestpractice; import java.util.List; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.TextView; public class talkAdapter extends ArrayAdapter<talk>{
private int resourceId; public talkAdapter(Context context, int resource, List<talk> objects) {
// TODO Auto-generated constructor stub
super(context,resource,objects);
this.resourceId = resource;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
talk t = getItem(position);
View v;
TextView tv;
if( null == convertView ){
v = LayoutInflater.from(getContext()).inflate(resourceId, null);
}
else{
v = convertView;
} if( t.getType() == t.TYPE_RECEIVED ) {
tv = (TextView) v.findViewById(R.id.left_msg);
v.findViewById(R.id.right_layout).setVisibility(View.GONE);
}
else{
tv = (TextView) v.findViewById(R.id.right_msg);
v.findViewById(R.id.left_layout).setVisibility(View.GONE);
}
tv.setText(t.getContent());
return v;
}
}
talkAdapter.java 为ListView自定义的适配器
<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"
android:orientation="vertical"
>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#0000"
>
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="input:"
/>
<EditText
android:id="@+id/edittext"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="Type something......."
android:maxLines="2"
/>
<Button
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="send"
/> </LinearLayout>
</LinearLayout>
activity_main.xml 聊天窗口
<?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定位到最后一行
觉得效果不对。(?)确实定位在新的数据那里了,但是原先的数据(除了第一条)都没了。