android异常整理

NullPointerException 空指针异常

  • 继承结构:
  • 代码:
    
    /**
     *
     * Thrown when an application attempts to use {@code null} in a
     * case where an object is required. These include:
     * <ul>
     * <li>Calling the instance method of a {@code null} object.
     * <li>Accessing or modifying the field of a {@code null} object.
     * <li>Taking the length of {@code null} as if it were an array.
     * <li>Accessing or modifying the slots of {@code null} as if it
     *     were an array.
     * <li>Throwing {@code null} as if it were a {@code Throwable}
     *     value.
     * </ul>
     * <p>
     * Applications should throw instances of this class to indicate
     * other illegal uses of the {@code null} object.
     *
     * {@code NullPointerException} objects may be constructed by the
     * virtual machine as if {@linkplain Throwable#Throwable(String,
     * Throwable, boolean, boolean) suppression were disabled and/or the
     * stack trace was not writable}.
     *
     * @author  unascribed
     * @since   JDK1.0
     */
      public class NullPointerException extends RuntimeException {
      private static final long serialVersionUID = 5162710183389028792L;
    
        /**
         * Constructs a {@code NullPointerException} with no detail message.
         */
        public NullPointerException() {
            super();
        }
    
        /**
         * Constructs a {@code NullPointerException} with the specified
         * detail message.
         *
         * @param   s   the detail message.
         */
        public NullPointerException(String s) {
            super(s);
        }
     }
    

IllegalArgumentException 参数不合法异常

UnsupportedOperationException 功能不支持异常

IndexOutOfBoundsException 数组下标越界异常

OutOfMemoryError 内存溢出异常

ClassCastException 类型转换异常

java.lang.StackOverflowError: stack size 8MB
这个错误是因为方法自己 调用自己。就是递归。

IncompatibleClassChangeError
出现这种错误的原因可能是因为jar包的代码发生了一些无法执行 二进制兼容 的变化。最有可能的是将非静态的成员变量或者方法变成了静态的,还有将私有的成员变量和方法变成了共有的。并且引用jar包的客户端没有重新编译源码

android.app.RemoteServiceException: Bad notification posted from package

android.view.ContextThemeWrapper cannot be cast to android.app.Activity

java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity

这个错误出现的情况是在Dialog中的context,在强制装换成Activity是会出现的,这种情况的解决方法:

	private static Activity scanForActivity(Context cont) {
		if (cont == null)
			return null;
		else if (cont instanceof Activity)
			return (Activity) cont;
		else if (cont instanceof ContextWrapper)
			return scanForActivity(((ContextWrapper) cont).getBaseContext());
 
		return null;
	}

其实也是一个强制转换,不过多添加了三个判断条件,第一个不说了,判断context不为空,第二个是判断context可不可以直接转换成Activity,可以的话直接强制转换,第三个则是这个判断context是不是属于ContextWrapper,是的话将context强制转换成ContextWrapper,在用这个方法判断一次。。能解决大部分情况的这种错误。

java.lang.IllegalStateException: Fragment(Fragment) not attached to Activity

报错位置一般是在getString(R.string.xx)

原因是fragment 还未attach时调用content

解决办法

调用时判断

if(isAdded()){
   getString(R.string.xx)
}

Calling startActivity() from outside of an Activity

当我们在**Adapter或者其他地方(如Service、BroadCastReceiver、Widget等)**中使用startActivity(),会报一个异常: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.

解决办法:这时需要为Intent设置一个Flag:FLAG_ACTIVITY_NEW_TASK

具体操作:Intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 这样就不会报错了,可以顺利的startActivity()。

在Activity中使用startActivity()的方法不会有任何限制,是因为Activity重载了Context的startActivity()方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值