安卓中出现过的一些容易被忽略的异常整理
作者:pigdreams
1.在外部开启activity时需要新开一个task,从service里开启activity时出现了这个异常。
W/System.err: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? W/System.err: at android.app.ContextImpl.startActivity(ContextImpl.java:944) W/System.err: at android.app.ContextImpl.startActivity(ContextImpl.java:931)
表示要添加一个Flag,建议的FLAG_ACTIVITY_NEW_TASK是一种activity启动方式,创建一个新的activity.
2.在setAdapter()之后加addHeaderView()会发生异常.
- When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time.
- KITKAT:October 2013: Android 4.4, KitKat, another tasty treat. android 4.4之后可以在任何地方调用,4.4之前的版本都会报错。
W/System.err: java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called. W/System.err: at android.widget.ListView.addHeaderView(ListView.java:257) W/System.err: at android.widget.ListView.addHeaderView(ListView.java:286)
3.TextView.setText()只能放charsequence类的参数,如果放的是整型数字,会报如下错误。
出现这个异常的原因的setText()里也可以放字符串资源id,如果放的是整形则会去R文件里找这个id对应的字符串,所以会出现NotFoundException的异常,即是找不到这个资源id所对应的文字。
注:CharSequence类的子类有String,StringBuffer,StringBuilder
E/InputEventReceiver: Exception dispatching input event. E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback E/MessageQueue-JNI: android.content.res.Resources$NotFoundException: String resource ID #0x28
4.spinner设置监听问题
如果用onItemClickListener会发生异常,异常如下。
setOnItemClickListener cannot be used with a spinner.
应该用onItemSelectedListener,这是一个很容易就会踩的坑。
W/System.err: java.lang.RuntimeException: Unable to start activity java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner. W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) W/System.err: at android.app.ActivityThread.access$600(ActivityThread.java:141) W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99) W/System.err: at android.os.Looper.loop(Looper.java:137) W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5041) W/System.err: at java.lang.reflect.Method.invokeNative(Native Method) W/System.err: at java.lang.reflect.Method.invoke(Method.java:511) W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) W/System.err: at dalvik.system.NativeStart.main(Native Method) W/System.err: Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
5.在service里调用progressDialog.show()方法时,会出现异常
如果没有将progress设为可悬浮在其它应用上方,并设置相应的权限,那么在service等其它组件中调用dialog.show()时,会无法获得当前windows,故无法正常显示dialog.
W/System.err: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application W/System.err: at android.view.ViewRootImpl.setView(ViewRootImpl.java:571) W/System.err: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246) W/System.err: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) W/System.err: at android.app.Dialog.show(Dialog.java:281)
//context传入为application progressDialog = new ProgressDialog(context); progressDialog.setMessage(msg); progressDialog.setCancelable(cancelable); progressDialog.setCanceledOnTouchOutside(false); progressDialog.show();
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接
您可能感兴趣的文章:
- Android跨进程抛异常的原理的实现
- Android7.0自动更新适配 包解析异常
- Android编程实现项目中异常捕获及对应Log日志文件保存功能
- Android Studio升级到3.0 Terminal 中文显示异常解决
- Android 全局异常捕获实例详解
- android 捕捉异常并上传至服务器的简单实现
- Android Studio下载更新Android SDK网络异常或无法下载
- Android 中Crash时如何获取异常信息详解及实例
- Android异常 java.lang.IllegalStateException解决方法
- AndroidStudio利用android-support-multidex解决64k的各种异常