這篇文章給大家分享的是有關(guān)android中容易被忽略的異常有哪些的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來,先為魯山等服務(wù)建站,魯山等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為魯山企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1.在外部開啟activity時(shí)需要新開一個(gè)task,從service里開啟activity時(shí)出現(xiàn)了這個(gè)異常。
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)
表示要添加一個(gè)Flag,建議的FLAG_ACTIVITY_NEW_TASK是一種activity啟動(dòng)方式,創(chuàng)建一個(gè)新的activity.
2.在setAdapter()之后加addHeaderView()會(huì)發(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之前的版本都會(huì)報(bào)錯(cuò)。
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ù)字,會(huì)報(bào)如下錯(cuò)誤。
出現(xiàn)這個(gè)異常的原因的setText()里也可以放字符串資源id,如果放的是整形則會(huì)去R文件里找這個(gè)id對(duì)應(yīng)的字符串,所以會(huì)出現(xiàn)NotFoundException的異常,即是找不到這個(gè)資源id所對(duì)應(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會(huì)發(fā)生異常,異常如下。
setOnItemClickListener cannot be used with a spinner.
應(yīng)該用onItemSelectedListener,這是一個(gè)很容易就會(huì)踩的坑。
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()方法時(shí),會(huì)出現(xiàn)異常
如果沒有將progress設(shè)為可懸浮在其它應(yīng)用上方,并設(shè)置相應(yīng)的權(quán)限,那么在service等其它組件中調(diào)用dialog.show()時(shí),會(huì)無法獲得當(dāng)前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)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!