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定位到最后一行

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

大四实习准备2_java异常处理_android控件练习的更多相关文章

  1. 大四实习准备1_java构造器_android ListView

    2015-4-23 Java构造器 与类名同名;无返回值(void也不行);被不同的修饰符修饰是有区别的;当构造函数被private修饰时,只有本类可访问.其他类可以通过该类的get函数得到对象.如单 ...

  2. 【风马一族_Android】第4章Android常用基本控件

    第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...

  3. Qt界面控件值获取异常处理

    情景简述: 正常情况,我们从控件获取的值是OK的,但有时候就是奇怪的不对头,那么我们可以给获取后的值加上一个不痛不痒的函数,再返回,结果就OK了.至于原因嘛,[呲牙][呲牙] 比如: //正常情况 d ...

  4. 浅尝辄止——使用ActiveX装载WPF控件

    1 引言 使用VC编写的容器类编辑器,很多都可以挂接ActiveX控件,因为基于COM的ActiveX控件不仅封装性不错,还可以显示一些不错的界面图元. 但是随着技术不断的进步,已被抛弃的Active ...

  5. WPF自定义控件与样式(11)-等待/忙/正在加载状态-控件实现

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要有三种实现方式 ...

  6. 多年前写的文本框扩展控件(有ValueChanging事件等),已放github

    本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 阅读目录 介绍 起因 代码 使用 GitHub ...

  7. BackgroundWorker控件

    在我们的程序中,经常会有一些耗时较长的运算,为了保证用户体验,不引起界面不响应,我们一般会采用多线程操作,让耗时操作在后台完成,完成后再进行处理或给出提示,在运行中,也会时时去刷新界面上的进度条等显示 ...

  8. Atitit.dwr3 不能显示错误详细信息的解决方案,控件显示错误详细信息的解决方案 java .net php

    Atitit.dwr3 不能显示错误详细信息的解决方案,控件显示错误详细信息的解决方案 java .net php 1. Keyword/subtitle 1 2. 使用dwr3的异常convert处 ...

  9. echart图表控件配置入门(二)常用图表数据动态绑定

    上一节 <echart图表控件配置入门(一)>介绍了echarts图表控件的入门配置,使开发人员可以快速搭建出一个静态的图表.但是在实际开发过程这还是不够的,不可能所有的图表控件都是静态数 ...

随机推荐

  1. 用JS写的无缝滚动特效

    代码如下 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  2. php session学习笔记(实例代码)

    http  无状态协议 一个服务器向客户端发送消息的时候有三条信息 一是状态二是头信息三是内容 会话控制 让一个用户访问每个页面,服务器都知道是哪个用户访问 cookie cookie是通过头信息发送 ...

  3. php语法检查方法——命令行模式和代码形式

    1. 命令行形式 php -l /path/to/file.php 2. php代码形式 function php_syntax_check($file){ $code = file_get_cont ...

  4. 《CSS3使用指南》读书笔记

    一.CSS3的来龙去脉 1.CSS3的新特性: 1)不依赖图片的视觉效果 2)盒容器变形 3)独一无二的字体 4)强大的选择器 5)过渡与动画 6)媒体信息查询 7)多列布局 2.CSS标准发布分5个 ...

  5. NodeJs环境部署

    node cli.js install npm -gf npm install express -gd

  6. 1036. Boys vs Girls (25)

    #include <stdio.h>#include <string.h>int main(){ int n,i; while(scanf("%d",&am ...

  7. keystone命令与client接口学习

    keystone学习 ------------------ Keystone(OpenStack Identity Service)是OpenStack框架中,负责身份验证.服务规则和服务令牌的功能, ...

  8. SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件

    路径说明: 一.加载类目录下的配置文件 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:ap ...

  9. 搭建 Android 开发环境,初试HelloWorld (win7) (上) (转)

    搭建Android开发环境主要有以下几步要做: 1.JDK安装 2.Eclipse安装 3.Android SDK安装 4.ADT安装 5.创建AVD 1.JDK(Java Development K ...

  10. case class inheritance

    Scala 禁止case class inheritance case class Person(name: String, age: Int) case class FootballPlayer(n ...