gpt4 book ai didi

android - 添加/删除 RelativeLayout(动画)的 View 时出现 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:40:50 24 4
gpt4 key购买 nike

您好,我想创建一个图像并从下到上制作动画,并将 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, 0, 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),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 个 View 并再次启动时,它因 NullPointerException 而崩溃,也许是并发问题?我该如何解决?

异常(exception):

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 是具有固定宽度和高度(以 dp 为单位)的 RelativeLayoutTools.toPx是DP转PX的方法

最佳答案

已解决,只需通知我正在删除 View 的处理程序:

handler.post(new Runnable() {
public void run() {
fogLayout.removeView(img);
}
});

关于android - 添加/删除 RelativeLayout(动画)的 View 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15636658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com