Android Exception异常汇集【不定时更新】
2014-12-16 10:06
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1161)
at android.app.ContextImpl.startActivity(ContextImpl.java:1148)
原因是:
Context中有一个startActivity方法,Activity继承自Context,重载了startActivity方法。如果使用 Activity的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的话,就需要开启一个新的task,遇到上面那个异常的,都是因为使用了Context的startActivity方法。解决办法是,加一个flag。intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2.
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS:
如果调出的Activtivity只是一个功能片段,并没有实际的意义,也没有必要出现在长按Home键调出最近使用过的程序类表中,那么使用FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
Intent intent = new Intent(this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
==========================================================================================
==========================================================================================
2015年01月12号
3.Android4.4.X版本,开启或关闭飞行模式时,报出异常:
虽然已经在AndroidManifest.xml声明了权限,但是依旧会报出异常。后查需要在AndroidManifest.xml 添加 android:sharedUserId="android.uid.system"
如果上面方法仍不奏效,那么可以使该apk的签名和平台保持一致。即使用源码平台默认签名对该apk进行重新签名,并push到 systen/app下面。那么操作Setttings表时就具有了系统权限。
==========================================================================================
==========================================================================================
2015年01月15日:
4.
/**
* Called when the database needs to be downgraded. This is strictly similar to
* {@link #onUpgrade} method, but is called whenever current version is newer than requested one.
* However, this method is not abstract, so it is not mandatory for a customer to
* implement it. If not overridden, default implementation will reject downgrade and
* throws SQLiteException
*
* <p>
* This method executes within a transaction. If an exception is thrown, all changes
* will automatically be rolled back.
* </p>
*
* @param db The database.
* @param oldVersion The old database version.
* @param newVersion The new database version.
*/
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
throw new SQLiteException("Can't downgrade database from version " +
oldVersion + " to " + newVersion);
}
==========================================================================================
==========================================================================================
2015年01月26日:
5. WindowManager$BadTokenException: Unable to add window…is not valid; is your activity running?
01-26 11:20:07.365 11836 11836 E AndroidRuntime: Process: com.mediatek.schpwronoff, PID: 11836
01-26 11:20:07.365 11836 11836 E AndroidRuntime: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41f06b68 is not valid; is your activity running?
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at android.view.ViewRootImpl.setView(ViewRootImpl.java:637)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at android.app.Dialog.show(Dialog.java:329)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at android.app.Activity.showDialog(Activity.java:3182)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at android.app.Activity.showDialog(Activity.java:3132)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at com.mediatek.schpwronoff.ShutdownActivity$1.onTick(ShutdownActivity.java:93)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:110)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5332)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
01-26 11:20:07.365 11836 11836 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
======================================================
FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4051c0e0 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:558)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:433)
at android.app.Dialog.show(Dialog.java:265)
at net.reecam.CameraSearch$3.onClick(CameraSearch.java:103)
at android.view.View.performClick(View.java:2532)
at android.view.View$PerformClick.run(View.java:9308)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
问题原因:
看报错关键词提示: Unable to add window 、LocalActivityManager、is your activity running,再结合应用程序中用到了AcitivityGroup来管理子Activity这个环境,而报错的前提就是在子Activity上弹出ProgressDialog进度条会话框,提示如上报错提示。就是说不能在子Activity上添加此窗口,LocalActivityManager$LocalActivityRecord@4051c0e0 is not valid就是说当前Activity管理器中此4051c0e0代表的Acitivity是无效的,也就是网上资料所说找不到它依附的Acitivity。
解决办法:
将new DialogToast(this);
改为:
new DialogToast(getParent());
就OK了。
是由于有activity时依附于另一个activity的,当被依附的activity产生错误的时候,该activity没有了靠山而产生错误(或者是调用了一个已经被finish()的activity)。一般在创建alertdialog.builder是很容易见到。
对于这个错误还没有根本的解决办法,唯一的办法就是改变该activity依附于另一activity的事实,将其放入另一activity。
ERROR/AndroidRuntime(10104): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@45a58ee0 is not valid; is your activity running?
原因分析:
因为new对话框的时候,参数content 指定成了this,即指向当前子Activity的content。但子Activity是动态创建的,不能保证一直存在。其父Activity的content是稳定存在的,所以有下面的解决办法。
解决办法:
将content替换为getParent()即可。
==========================================================================================
==========================================================================================
2015年01月27
6.android.os.BadParcelableException: ClassNotFoundException when unmarshalling:解决办法
例如在用AlarmManager的时候
1 AlarmManager alarmMgr = (AlarmManager) mContext
2 .getSystemService(Context.ALARM_SERVICE);
3 Intent intent = new Intent(ALARM_ALERT);
4 // intent.setExtrasClassLoader(DBTask.class.getClassLoader());
5 Bundle mBundle=new Bundle();
6 mBundle.putParcelable(AlarmMeReceiver.DB_TASK_KEY,task);
7 intent.putExtras(mBundle);
8 int id=Integer.valueOf(task.id);
9 PendingIntent pendIntent = PendingIntent.getBroadcast(
10 mContext.getApplicationContext(), id, intent,
11 PendingIntent.FLAG_UPDATE_CURRENT);
12 long triggerAtTime = task.time;
13 alarmMgr.set(AlarmManager.RTC_WAKEUP , triggerAtTime, pendIntent);
通过第6行 intent传提一个Parcelable对象 (Parcelable里面没有问题)
报android.os.BadParcelableException: ClassNotFoundException when unmarshalling错误可以加上
intent.setExtrasClassLoader(DBTask.class.getClassLoader());
原因是android.platform.frameworks.base/core/java/android/content/Intent.java
5052 try {
5053 Bundle newb = new Bundle(other.mExtras);
5054 newb.putAll(mExtras);
5055 mExtras = newb;
5056 } catch (RuntimeException e) {
5057 // Modifying the extras can cause us to unparcel the contents
5058 // of the bundle, and if we do this in the system process that
5059 // may fail. We really should handle this (i.e., the Bundle
5060 // impl shouldn't be on top of a plain map), but for now just
5061 // ignore it and keep the original contents. :(
5062 Log.w("Intent", "Failure filling in extras", e);
5063 }
android 中自定义的对象序列化的问题有两个选择一个是Parcelable,另外一个是Serializable。
一 序列化原因:
1.永久性保存对象,保存对象的字节序列到本地文件中;
2.通过序列化对象在网络中传递对象;
3.通过序列化在进程间传递对象。
二 至于选取哪种可参考下面的原则:
1.在使用内存的时候,Parcelable 类比Serializable性能高,所以推荐使用Parcelable类。
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但在这种情况下,还是建议你用Serializable 。
==============================================================================================================
android.os.BadParcelableException: ClassNotFoundException when unmarshalling
=========================================================================================
关于这个“Activity之间对象传递的两种方法(Serializable,Parcelable)”我是通过别人的博客里学习到的,引用自:http://blog.csdn.net/Android_Tutor/archive/2010/07/16/5740845.aspx。
首先,我这里主要说下Parcelable 的用法。
1、定义对象类,类实现Parcelable接口。且要实现Parcelable接口的几个必要方法。如我的Question类中的相关方法的实现:
private Question(Parcel in){
}
public static final Question.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {
};
@Override
public void writeToParcel(Parcel out, int flags) {
}
注意:
方法private Question(Parcel in){}和方法public void writeToParcel(Parcel out, int flags) {}中变量顺序是一致的,是一一对应的。其中,
2、程序中的应用:
1)存储
2)提取
3)接下来就可以使用question对象了。
其次,在使用Parcelable的过程中,曾报异常:
Caused By:android.os.BadParcelableException: ClassNotFoundException when unmarshalling:
该异常是由于,我在对象类得定义中,成员变量的读取和存储没有一一对应,导致成员变量赋值错误,从而导致上述异常,所以,在上述对象类的定义说明中加了“注意:”