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

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

大四实习准备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. Node.js 【使用npm安装一些包失败之笔记】

    镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 1.通过config命令 npm config set registry https://regist ...

  2. ldd查看可执行程序依赖的文件

    ldd 用于查看可执行程序依赖的so动态链接库文件 [root@localhost ld.so.conf.d]# ldd /usr/local/tengine/sbin/nginx linux-vds ...

  3. window下安装composer和laravel

    安装composer: 1.在https://getcomposer.org/download/ 中下载 Composer-Setup.exe 2.安装composer步骤如下: 至此,compose ...

  4. 1072. Gas Station (30)

    先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...

  5. 1043. Is It a Binary Search Tree

    http://www.patest.cn/contests/pat-a-practise/1043 #include <stdio.h> #include <vector> u ...

  6. 快捷设置IE代理小工具

    时间:2015-02-06 起因: 公司新装了PLM系统,用这个系统必须使用指定IP段的IP才能访问.所以为了还能愉快的继续使用代理进行特定网站的访问,我们必须要频繁的去设置IE代理,这也太麻烦了吧. ...

  7. EXTJS 4.2 资料 控件GroupingGrid

    http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-17/179.html

  8. EasyUI + EF + MVC4 后台截图

    到目前完成的页面截图,完成了增删改查几项功能的技术测试,在解决几个小问题,就重新设计结构开始一个完整的后台开发,坚持用博客和云笔记记录开发过程.

  9. 监控SQL Server的job执行情况

    在服务器没有设置发邮件并且不允许发邮件的情况下, 可以通过下列语句来检查SQL Server 的job的执行情况 select top 150 a.run_date,a.run_time, b.nam ...

  10. python学习笔记19(序列的方法)

    序列包含有宝值 表(tuple)和表(list).此外,字符串(string)是一种特殊的定值表,表的元素可以更改,定值表一旦建立,其元素不可更改. 任何的序列都可以引用其中的元素(item). 下面 ...