嗨,我想从下到上创建一个图像和动画,并将alpha从1.0更改为0.0.我有这个代码(我在onCreate方法中调用3次来制作3个动画图像):
/** * 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的方法