您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。