安卓中出现过的一些容易被忽略的异常整理
作者:pigdreams 时间:2023-12-17 06:27:53
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();
来源:https://blog.csdn.net/pigdreams/article/details/54576931
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android开发中多进程共享数据简析
OpenGL绘制Bezier曲线的方法
Android Studio中如何修改APP图标和APP名称
![](https://img.aspxhome.com/file/2023/1/125361_0s.png)
SpringBoot 实现动态添加定时任务功能
![](https://img.aspxhome.com/file/2023/6/102646_0s.png)
Android编程简易实现XML解析的方法详解
![](https://img.aspxhome.com/file/2023/3/88663_0s.jpg)
Spring Boot2.0中SpringWebContext找不到无法使用的解决方法
![](https://img.aspxhome.com/file/2023/1/72661_0s.jpg)
Java中的collection集合类型总结
![](https://img.aspxhome.com/file/2023/4/122954_0s.jpg)
android 网络编程之网络通信几种方式实例分享
Android获取手机系统版本等信息的方法
学习Java之如何正确地向上转型与向下转型
![](https://img.aspxhome.com/file/2023/4/96064_0s.webp)
android 修改launcher行数和列数的方法
![](https://img.aspxhome.com/file/2023/1/139351_0s.png)
Unity实现旋转扭曲图像特效
![](https://img.aspxhome.com/file/2023/2/111272_0s.png)
c#批量上传图片到服务器示例分享
springmvc如何使用map接收参数
![](https://img.aspxhome.com/file/2023/9/60289_0s.png)
深入理解Java抽象类
Android多国语言转换Excel及Excel转换为string详解
![](https://img.aspxhome.com/file/2023/1/110761_0s.png)
IntelliJ IDEA如何设置JDK版本
![](https://img.aspxhome.com/file/2023/7/126337_0s.png)
java实现清理DNS Cache的方法
Android仿京东分类模块左侧分类条目效果
![](https://img.aspxhome.com/file/2023/4/130354_0s.jpg)