ANDROID开发中,新手们常常碰到的bug和异常

1.空指针异常 java.lang.NullPointerException

这个异常,是我们在软件开发中最简单,而且最容易犯的错误,一般遇到空指针的时候有如下几种情况:

一.想要使用的控件没有初始化,或者调用了不存在的对象,废话不多说,看代码:

	public class MainActivity extends AppCompatActivity {

    		private TextView textView;

    		@Override
    		protected void onCreate(Bundle savedInstanceState) {
        		super.onCreate(savedInstanceState);
        		setContentView(R.layout.activity_main);

        		initView();
    		}

    		private void initView() {
		//textView = (TextView) findViewById(R.id.tv); 加上这句就ok了
        	textView.setText("空指针异常");
    		}
	}
	这个时候,我没有对控件进行初始化而进行赋值操作,会报 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.set		 Text(java.lang.CharSequence)' on a null object reference
二.数组空指针异常:
	private int[] str1;
	private int[] str2 = new int[]{1,2,3};
	//str1 = new int[3];前面加上这句话,这个数组赋值就不会空指针异常了
	str1[0] = str2[0];//这里报了空指针异常,
2.数据类型转换异常 java.lang.ClassCastException
工作中,我们在数据类型转换的时候,常常碰到这样的异常,看代码:
布局文件:
	<?xml version="1.0" encoding="utf-8"?>
	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    		xmlns:tools="http://schemas.android.com/tools"
    		android:id="@+id/activity_main"
    		android:layout_width="match_parent"
    		android:layout_height="match_parent"
    		android:paddingBottom="@dimen/activity_vertical_margin"
    		android:paddingLeft="@dimen/activity_horizontal_margin"
    		android:paddingRight="@dimen/activity_horizontal_margin"
    		android:paddingTop="@dimen/activity_vertical_margin"
    		tools:context="bwhx.myapplication.MainActivity">
    		//看这里
    		<TextView
        		android:id="@+id/tv"
        		android:layout_width="wrap_content"
        		android:layout_height="wrap_content"
        		android:text="Hello World!" />
	</RelativeLayout>
代码:
	private EditText textView;//看里面

	@Override
	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);

    		initView();
	}

	private void initView() {
    		textView = (EditText) findViewById(R.id.tv);
    		textView.setText("这是类型转换异常");

	}
通过代码很容易就看出,我布局文件使用的是TextView,而我代码里面初始化的时候却是EditText,所有就报了异常:java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText

3.数组下标越界异常 java.lang.ArrayIndexOutOfBoundsException
在开发中,我们经常会用到数组,有时候我们在使用的时候不注意或者不小心的时候,想获取一个数组的值赋值给另一个数组,这个时候,我们要看下要赋值的这个数组是否有足够的空间,如果被赋值的这个数组的length没有临时数组的length长,那么就会出现数组下标越界,例如:
private int[] str1 = new int[3];
private int[] str2 = new int[]{1, 2, 3, 4, 5};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initView();
}

private void initView() {

    for (int i = 0; i < str2.length; i++) {
        str1[i] = str2[i];
        System.out.println(str1[i]);
    }

}
很明显的看出,这样做赋值,肯定会出现数组下标越界的bug
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
 at bwhx.myapplication.MainActivity.initView(MainActivity.java:28)
 at bwhx.myapplication.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 

4.找不到activity content.ActivityNotFoundException
这个异常其实是我们操作上的规范,有些人创建一个类虽然继续了Activity,但是却忘了在AndroidManifest里面进行注册,这个时候就会报这个错误,
android.content.ActivityNotFoundException: Unable to find explicit activity class {bwhx.myapplication/bwhx.myapplication.TestActivity}; have you declared this activity in your AndroidManifest.xml?

5.SQLException:操作数据库异常类。
在操作数据库的时候,比如我们需要向数据里面插入一个字段,如果数据里面存在了这个字段的话,我们再插入的时候会报这个异常,大家要记住,对数据库操作的时候,当操作结束的时候不要忘记关闭数据库cursor.close,官网上对于数据库异常介绍的比较笼统,想要仔细研究数据库的朋友,可以去看下源码

最后向那些初学者介绍下,如果查看你的程序出现的异常:首先打开你软件的Android Monitor(监控器,此处以AndroidStudio为例),打开你的Logcat(日志),选中你的
ERROR(错误),然后再找到Caused by,快速定位到你的错误
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
 	at bwhx.myapplication.MainActivity.initView(MainActivity.java:28)
 	at bwhx.myapplication.MainActivity.onCreate(MainActivity.java:20)
	at android.app.Activity.performCreate(Activity.java:6237)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
	at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 	at android.os.Handler.dispatchMessage(Handler.java:102) 
	at android.os.Looper.loop(Looper.java:148) 
	at android.app.ActivityThread.main(ActivityThread.java:5417) 

                               
写这篇文章主要是网上有个朋友刚开始学习android,不懂得怎么区分和解决常见的异常错误,这篇文章主要对那些刚入行的朋友看的,大神们不要喷,以后会经常更新我的博客,不足的地方多多指点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值