关于android开发使用onContextItemSelected方法的异常处理
错误代码 :android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
报错如下截图:
错误原因如下图所示:
因为在项目使用了onContextItemSelected方法,return返回使用了默认super父类方法super.onContextItemSelected(item),所以导致报错:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
解决办法
方法1:修改return默认值true
方法2:把switch的break中断改为return true,结束switch语句并且跳出当前函数,不在执行后面的语句。
原理是super.onContextItemSelected(item)默认返回是false,表示交给系统自己处理该事件,此时系统会调用ComtextImpl.startActivity()方法,所以就报错了。