1. 程式人生 > >android4.0.4 dispatchDraw NullPointerException

android4.0.4 dispatchDraw NullPointerException

出現的異常狀況:

03-10 15:39:41.386: D/AndroidRuntime(8000): Shutting down VM
03-10 15:39:41.386: W/dalvikvm(8000): threadid=1: thread exiting with uncaught exception (group=0x40c2e1f8)
03-10 15:39:41.396: E/AndroidRuntime(8000): FATAL EXCEPTION: main
03-10 15:39:41.396: E/AndroidRuntime(8000): java.lang.NullPointerException
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2496)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.view.View.draw(View.java:11183)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.widget.FrameLayout.draw(FrameLayout.java:450)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2295)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.view.ViewRootImpl.draw(ViewRootImpl.java:2231)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1837)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2649)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.os.Looper.loop(Looper.java:137)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at android.app.ActivityThread.main(ActivityThread.java:4512)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at java.lang.reflect.Method.invoke(Method.java:511)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
03-10 15:39:41.396: E/AndroidRuntime(8000): 	at dalvik.system.NativeStart.main(Native Method)

異常程式碼:

@Override
	public void onAnimationEnd(Animation animation) {
		// TODO Auto-generated method stub		
		mRTL_Tag.setVisibility(View.INVISIBLE);
		((RelativeLayout) mRTL_Ctl.getParent()).removeView(mRTL_Tag);
	}


這個問題出現的很詭異,在android 4.1.X以上沒有發現有這個問題,4.0.4有發生,但是沒有cause by的程式碼位置。

所以出現這個問題的時候不太好定位.建議出現這個問題的時候檢查一下animation和removeView的相關程式碼即可。

網上相關: http://dashasalo.com/2013/09/16/android-removeview-crashes-animation-listener/

UI Handler  post 更新可解決。

@Override
public void onAnimationEnd(Animation animation) {
	// TODO Auto-generated method stub		
	mRTL_Tag.setVisibility(View.INVISIBLE);
	new Handler().post(new Runnable() {
		public void run() {
			((RelativeLayout) mRTL_Ctl.getParent()).removeView(mRTL_Tag);
		}
	});
}