大佬教程收集整理的这篇文章主要介绍了android – 添加/删除RelativeLayout视图时的NullPointerException(动画),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
/** * Starts to make fog */ private void startFogGenerator() { handler.postDelayed(new Runnable() { @Override public void run() { final ImageView img = new ImageView(Activity.this); Boolean showN1 = r.nextBoolean(); if (showN1) { img.setImageresource(R.drawable.nube_01); } else { img.setImageresource(R.drawable.nube_02); } Animation animation = AnimationUtils.loadAnimation(Activity.this,R.anim.translate_and_alpha); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { fogLayout.removeView(img); fogLayout.invalidate(); // always true? Maybe but just check for for concurrency safe. if (setFogN(false) < MAX_CLOUDS_ON_SCREEN) { startFogGenerator(); } } @Override public void onAnimationRepeat(Animation animation) { } }); int pxWidth = Tools.toPx(140,getresources()); // Width of layout container int paddingWidth = Tools.toPx(20,getresources()); int x = r.nexTint(pxWidth - paddingWidth - paddingWidth) + paddingWidth; int range = Tools.toPx(50,getresources()); int width; int height; if (showN1) { // nube01: 167x226 width = r.nexTint(Tools.toPx(167,getresources()) - rangE) + range; height= r.nexTint(Tools.toPx(226,getresources()) - rangE) + range; } else { // nube01: 144x177 width = r.nexTint(Tools.toPx(144,getresources()) - rangE) + range; height= r.nexTint(Tools.toPx(177,getresources()) - rangE) + range; } Log.d("Animation","X: " + x + ",Width: " + width + ",Height: " + height); //img.setPadding(x,0); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height); params.leftMargin = x; fogLayout.addView(img,params); img.setBACkgroundColor(Color.argb(255,r.nexTint(255),r.nexTint(255))); // just for debug img.startAnimation(animation); setFogN(true); } },r.nexTint(2500 - 200) + 200); } private final static int MAX_CLOUDS_ON_SCREEN = 3; private AtomicInteger currentFogs = new AtomicInteger(0); private synchronized int setFogN(Boolean increment) { int toReturn = increment ? currentFogs.incrementAndGet() : currentFogs.decrementAndGet(); Log.d("TeaAnim","ToReturn: "+ toReturn); return toReturn; }
工作正常但是当删除第3个视图并开始另一个时间时,它会崩溃并出现NullPointerException,可能是并发问题?我怎么解决它?
例外:
ERROR/AndroidRuntime(10736): FATAL EXCEPTION: main java.lang.NullPointerException at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2796) at android.view.View.getDisplayList(View.java:12648) at android.view.View.getDisplayList(View.java:12694) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910) at android.view.View.getDisplayList(View.java:12588) at android.view.View.getDisplayList(View.java:12694) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910) at android.view.View.getDisplayList(View.java:12588) at android.view.View.getDisplayList(View.java:12694) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910) at android.view.View.getDisplayList(View.java:12588) at android.view.View.getDisplayList(View.java:12694) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910) at android.view.View.getDisplayList(View.java:12588) at android.view.View.getDisplayList(View.java:12694) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2910) at android.view.View.getDisplayList(View.java:12588) at android.view.View.getDisplayList(View.java:12694) at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1198) at android.view.ViewRootImpl.draw(ViewRootImpl.java:2173) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) at android.view.Choreographer$CallBACkRecord.run(Choreographer.java:749) at android.view.Choreographer.doCallBACks(Choreographer.java:562) at android.view.Choreographer.doFrame(Choreographer.java:532) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) at android.os.Handler.handleCallBACk(Handler.java:725) at android.os.Handler.dispatchmessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5204) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) at dalvik.system.NativeStart.main(Native Method)
笔记:
fogLayout是一个具有固定宽度和高度的RelativeLayout(以dp为单位)
Tools.toPx是一种从DP转换为PX的方法
以上是大佬教程为你收集整理的android – 添加/删除RelativeLayout视图时的NullPointerException(动画)全部内容,希望文章能够帮你解决android – 添加/删除RelativeLayout视图时的NullPointerException(动画)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。