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

2015-4-24

Java 异常处理

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

异常分类

1>继承关系

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

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

2>

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

未捕获异常

;

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

 1 public class test{
 2     public static void main(String[] args) {
 3         try{
 4             test t = new test();
 5             t.a();
 6         }catch(Exception e){
 7                        //在这里处理异常
 8             System.out.println("processd in main()");
 9         }
10     }
11     private void a(){
12         b();
13     }
14     private void b(){
15         int[] r = new int[5];
16         r[5]=10;//在这里发生异常
17     }
18 }        

或者 (?)使用throw、throws。

;

自定义异常

 1 //继承自Exception,写了两个构造函数。可以参考Exception类的代码
 2 class myException extends Exception{
 3     public myException() {
 4         // TODO Auto-generated constructor stub
 5     }
 6     public myException(String s){
 7         super(s);
 8     }
 9 }
10 
11 public class test{
12     //定义一个可能发生自定义异常的方法
13     public String yiChang(int x) throws myException{
14         if( x>0 ){
15             return "Right";
16         }
17         else{
18             throw new myException("Negavite");
19         }
20     }
21     public static void main(String[] args){
22         test t1 = new test();
23         
24         try{
25             System.out.println(t1.yiChang(5));
26         }catch(myException e){
27             System.out.println("异常信息为:"+e.getMessage());
28         }
29         
30         try{
31             System.out.println(t1.yiChang(-2));
32         }catch(myException e){
33             System.out.println("异常信息为:"+e.getMessage());
34             //关注这些方法e.getMessage(),e.toString(),e.printStackTrace()
35         }
36     }
37 }
自定义异常示例

android 控件练习UIBestPractice

 1 package com.example.uibestpractice;
 2 
 3 import java.util.ArrayList;
 4 
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.view.Menu;
 8 import android.view.MenuItem;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 import android.widget.EditText;
13 import android.widget.ListView;
14 import android.widget.TextView;
15 
16 public class MainActivity extends Activity {
17     ArrayList<talk> list = new ArrayList<talk>();
18     ListView lv;
19     EditText et;
20     Button bt;
21     talkAdapter adapter;
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity_main);
26         inittalk();
27         adapter = new talkAdapter(MainActivity.this, R.layout.diy_left_right, list);
28         lv = (ListView) findViewById(R.id.listview);
29         lv.setAdapter(adapter);
30         et = (EditText) findViewById(R.id.edittext);
31         bt = (Button) findViewById(R.id.button);
32         bt.setOnClickListener(new OnClickListener() {
33             
34             @Override
35             public void onClick(View view) {
36                 // TODO Auto-generated method stub
37                 String content = et.getText().toString();
38                 if( !content.equals("") ){
39                     talk t = new talk(content,talk.TYPE_SEND);
40                     list.add(t);
41                     adapter.notifyDataSetChanged();//当有新消息是,刷新ListView的显示
42                     lv.setSelection(list.size());//将ListView定位到最后一行
43                     et.setText(""+list.size());//清空输入框中的内容
44                 }
45             }
46         });
47     }
48     private void inittalk() {
49          //TODO Auto-generated method stub
50         talk t = new talk("hello kiwi",talk.TYPE_RECEIVED);
51         list.add(t);
52         t = new talk("hello tryonce",talk.TYPE_SEND);
53         list.add(t);
54         t = new talk("Come on together",talk.TYPE_RECEIVED);
55         list.add(t);
56         t = new talk("It's my honor,my dearling",talk.TYPE_SEND);
57         list.add(t);
58     }
59 }
MainActivity.java
 1 package com.example.uibestpractice;
 2 
 3 public class talk {
 4     public static final int TYPE_RECEIVED = 0;
 5     public static final int TYPE_SEND = 1;
 6     private String s;
 7     private int type;
 8     public talk(String s,int type){
 9         this.s = s;
10         this.type = type;
11     }
12     public String getContent(){
13         return s;
14     }
15     public int getType(){
16         return type;
17     }
18 }
talk.java ListView的数据类
 1 package com.example.uibestpractice;
 2 
 3 import java.util.List;
 4 
 5 import android.content.Context;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9 import android.webkit.WebView.FindListener;
10 import android.widget.ArrayAdapter;
11 import android.widget.TextView;
12 
13 public class talkAdapter extends ArrayAdapter<talk>{
14     private int resourceId;
15     
16     public talkAdapter(Context context, int resource, List<talk> objects) {
17         // TODO Auto-generated constructor stub
18         super(context,resource,objects);
19         this.resourceId = resource;
20     }
21     
22     @Override
23     public View getView(int position, View convertView, ViewGroup parent) {
24         // TODO Auto-generated method stub
25         talk t = getItem(position);
26         View v;
27         TextView tv;
28         if( null == convertView ){
29             v = LayoutInflater.from(getContext()).inflate(resourceId, null);
30         }
31         else{
32             v = convertView;
33         }
34         
35         if( t.getType() == t.TYPE_RECEIVED )    {
36             tv = (TextView) v.findViewById(R.id.left_msg);
37             v.findViewById(R.id.right_layout).setVisibility(View.GONE);
38         }
39         else{
40             tv = (TextView) v.findViewById(R.id.right_msg);
41             v.findViewById(R.id.left_layout).setVisibility(View.GONE);
42         }
43         tv.setText(t.getContent());
44         return v;
45     }
46 }
talkAdapter.java 为ListView自定义的适配器
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6      >
 7     <ListView 
 8         android:id="@+id/listview"
 9         android:layout_width="match_parent"
10         android:layout_height="0dp"
11         android:layout_weight="1"
12         android:divider="#0000"
13         >
14     </ListView>
15     <LinearLayout 
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:orientation="horizontal"
19         >
20         <TextView 
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:text="input:"
24             />
25         <EditText 
26             android:id="@+id/edittext"
27             android:layout_height="wrap_content"
28             android:layout_width="0dp"
29             android:layout_weight="1"
30             android:hint="Type something......."
31             android:maxLines="2"
32             />
33         <Button 
34             android:id="@+id/button"
35             android:layout_height="wrap_content"
36             android:layout_width="wrap_content"
37             android:text="send"
38             />
39         
40     </LinearLayout>
41 </LinearLayout>
activity_main.xml 聊天窗口
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     <LinearLayout 
 7         android:id="@+id/left_layout"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:layout_gravity="left"
11         android:background="@drawable/left">
12         
13         <TextView 
14             android:id="@+id/left_msg"
15             android:layout_width="wrap_content"
16             android:layout_height="wrap_content"
17             android:layout_gravity="center"
18             android:layout_margin="10dp"
19             android:textColor="#fff"
20             />
21      </LinearLayout>
22      
23         <LinearLayout 
24         android:id="@+id/right_layout"
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:layout_gravity="right"
28         android:background="@drawable/right">
29         
30         <TextView 
31             android:id="@+id/right_msg"
32             android:layout_width="wrap_content"
33             android:layout_height="wrap_content"
34             android:layout_gravity="center"
35             android:layout_margin="10dp"
36             />
37      </LinearLayout>
38 
39 </LinearLayout>
diy_left_right.xml 为ListView自定义的布局

问题:

MainActivity里 

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

觉得效果不对。(?)确实定位在新的数据那里了,但是原先的数据(除了第一条)都没了。

原文地址:https://www.cnblogs.com/kiwi-bird/p/4454300.html