首页 > 系统 > Android > 正文

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

2019-10-21 21:32:04
字体:
来源:转载
供稿:网友

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: handleReceiveCallbackE/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 applicationW/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();

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对VEVB武林网的支持。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表