欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

安卓中出现过的一些容易被忽略的异常整理

程序员文章站 2022-08-20 16:28:06
1.在外部开启activity时需要新开一个task,从service里开启activity时出现了这个异常。 w/system.err: android.uti...

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();

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接