您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)android中容易被忽略的異常有哪些的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
1.在外部開啟activity時需要新開一個task,從service里開啟activity時出現(xiàn)了這個異常。
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啟動方式,創(chuàng)建一個新的activity.
2.在setAdapter()之后加addHeaderView()會發(fā)生異常.
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之后可以在任何地方調(diào)用,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類的參數(shù),如果放的是整型數(shù)字,會報如下錯誤。
出現(xiàn)這個異常的原因的setText()里也可以放字符串資源id,如果放的是整形則會去R文件里找這個id對應(yīng)的字符串,所以會出現(xiàn)NotFoundException的異常,即是找不到這個資源id所對應(yīng)的文字。
注: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設(shè)置監(jiān)聽問題
如果用onItemClickListener會發(fā)生異常,異常如下。
setOnItemClickListener cannot be used with a spinner.
應(yīng)該用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里調(diào)用progressDialog.show()方法時,會出現(xiàn)異常
如果沒有將progress設(shè)為可懸浮在其它應(yīng)用上方,并設(shè)置相應(yīng)的權(quán)限,那么在service等其它組件中調(diào)用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();
感謝各位的閱讀!關(guān)于“android中容易被忽略的異常有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。