您好,登录后才能下订单哦!
这篇文章给大家分享的是有关android中容易被忽略的异常有哪些的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
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中容易被忽略的异常有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。