大四实习准备2_java异常处理_android控件练习
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_java构造器_android ListView
2015-4-23 Java构造器 与类名同名;无返回值(void也不行);被不同的修饰符修饰是有区别的;当构造函数被private修饰时,只有本类可访问.其他类可以通过该类的get函数得到对象.如单 ...
- 【风马一族_Android】第4章Android常用基本控件
第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...
- Qt界面控件值获取异常处理
情景简述: 正常情况,我们从控件获取的值是OK的,但有时候就是奇怪的不对头,那么我们可以给获取后的值加上一个不痛不痒的函数,再返回,结果就OK了.至于原因嘛,[呲牙][呲牙] 比如: //正常情况 d ...
- 浅尝辄止——使用ActiveX装载WPF控件
1 引言 使用VC编写的容器类编辑器,很多都可以挂接ActiveX控件,因为基于COM的ActiveX控件不仅封装性不错,还可以显示一些不错的界面图元. 但是随着技术不断的进步,已被抛弃的Active ...
- WPF自定义控件与样式(11)-等待/忙/正在加载状态-控件实现
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要有三种实现方式 ...
- 多年前写的文本框扩展控件(有ValueChanging事件等),已放github
本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 阅读目录 介绍 起因 代码 使用 GitHub ...
- BackgroundWorker控件
在我们的程序中,经常会有一些耗时较长的运算,为了保证用户体验,不引起界面不响应,我们一般会采用多线程操作,让耗时操作在后台完成,完成后再进行处理或给出提示,在运行中,也会时时去刷新界面上的进度条等显示 ...
- Atitit.dwr3 不能显示错误详细信息的解决方案,控件显示错误详细信息的解决方案 java .net php
Atitit.dwr3 不能显示错误详细信息的解决方案,控件显示错误详细信息的解决方案 java .net php 1. Keyword/subtitle 1 2. 使用dwr3的异常convert处 ...
- echart图表控件配置入门(二)常用图表数据动态绑定
上一节 <echart图表控件配置入门(一)>介绍了echarts图表控件的入门配置,使开发人员可以快速搭建出一个静态的图表.但是在实际开发过程这还是不够的,不可能所有的图表控件都是静态数 ...
随机推荐
- 用JS写的无缝滚动特效
代码如下 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...
- php session学习笔记(实例代码)
http 无状态协议 一个服务器向客户端发送消息的时候有三条信息 一是状态二是头信息三是内容 会话控制 让一个用户访问每个页面,服务器都知道是哪个用户访问 cookie cookie是通过头信息发送 ...
- php语法检查方法——命令行模式和代码形式
1. 命令行形式 php -l /path/to/file.php 2. php代码形式 function php_syntax_check($file){ $code = file_get_cont ...
- 《CSS3使用指南》读书笔记
一.CSS3的来龙去脉 1.CSS3的新特性: 1)不依赖图片的视觉效果 2)盒容器变形 3)独一无二的字体 4)强大的选择器 5)过渡与动画 6)媒体信息查询 7)多列布局 2.CSS标准发布分5个 ...
- NodeJs环境部署
node cli.js install npm -gf npm install express -gd
- 1036. Boys vs Girls (25)
#include <stdio.h>#include <string.h>int main(){ int n,i; while(scanf("%d",&am ...
- keystone命令与client接口学习
keystone学习 ------------------ Keystone(OpenStack Identity Service)是OpenStack框架中,负责身份验证.服务规则和服务令牌的功能, ...
- SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件
路径说明: 一.加载类目录下的配置文件 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:ap ...
- 搭建 Android 开发环境,初试HelloWorld (win7) (上) (转)
搭建Android开发环境主要有以下几步要做: 1.JDK安装 2.Eclipse安装 3.Android SDK安装 4.ADT安装 5.创建AVD 1.JDK(Java Development K ...
- case class inheritance
Scala 禁止case class inheritance case class Person(name: String, age: Int) case class FootballPlayer(n ...