从 Android 中的 LinearLayout 中删除视图时出现异常
我尝试在将自定义视图从 LinearLayout 容器中删除之前对其进行动画处理。只是我不断收到 NullPointerException,我无法追踪到任何特定的位置。
当我添加两个视图并删除第一个视图时,会发生异常。
我的代码是:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_alg);
fadeInAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.witness_fade_in);
list = (LinearLayout) this.findViewById(R.id.alg_list_getuigen);
addWitness = (ImageButton) this
.findViewById(R.id.alg_getuige_toevoegen);
addWitness.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final View w = View.inflate(getApplicationContext(),
R.layout.form_getuige, null);
ImageButton button = (ImageButton) w
.findViewById(R.id.getuige_verwijderen);
button.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "deleteView");
Animation fadeOutAnimation = AnimationUtils
.loadAnimation(getApplicationContext(),
R.anim.witness_fade_out);
fadeOutAnimation
.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(
Animation animation) {
list.removeView(w);
list.invalidate();
}
@Override
public void onAnimationRepeat(
Animation animation) {
}
@Override
public void onAnimationStart(
Animation animation) {
}
});
w.setVisibility(View.VISIBLE);
w.startAnimation(fadeOutAnimation);
w.setVisibility(View.INVISIBLE);
}
});
Log.d(TAG, "addView");
w.setVisibility(View.INVISIBLE);
list.addView(w);
w.startAnimation(fadeInAnimation);
w.setVisibility(View.VISIBLE);
list.invalidate();
}
});
}
这是堆栈跟踪:
07-03 03:17:30.335: WARN/dalvikvm(343): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): FATAL EXCEPTION: main
07-03 03:17:30.365: ERROR/AndroidRuntime(343): java.lang.NullPointerException
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1372)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.View.draw(View.java:6986)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.widget.ScrollView.draw(ScrollView.java:1409)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.View.draw(View.java:6883)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.View.draw(View.java:6883)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewRoot.draw(ViewRoot.java:1522)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.os.Looper.loop(Looper.java:123)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at java.lang.reflect.Method.invoke(Method.java:507)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at dalvik.system.NativeStart.main(Native Method)
Im trying to animate a custom view before removing it from a LinearLayout container. Only I keep getting a NullPointerException which I can't trace to anywhere specific.
The exception happens when I have added two views and remove the first one.
My code is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_alg);
fadeInAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.witness_fade_in);
list = (LinearLayout) this.findViewById(R.id.alg_list_getuigen);
addWitness = (ImageButton) this
.findViewById(R.id.alg_getuige_toevoegen);
addWitness.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final View w = View.inflate(getApplicationContext(),
R.layout.form_getuige, null);
ImageButton button = (ImageButton) w
.findViewById(R.id.getuige_verwijderen);
button.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "deleteView");
Animation fadeOutAnimation = AnimationUtils
.loadAnimation(getApplicationContext(),
R.anim.witness_fade_out);
fadeOutAnimation
.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(
Animation animation) {
list.removeView(w);
list.invalidate();
}
@Override
public void onAnimationRepeat(
Animation animation) {
}
@Override
public void onAnimationStart(
Animation animation) {
}
});
w.setVisibility(View.VISIBLE);
w.startAnimation(fadeOutAnimation);
w.setVisibility(View.INVISIBLE);
}
});
Log.d(TAG, "addView");
w.setVisibility(View.INVISIBLE);
list.addView(w);
w.startAnimation(fadeInAnimation);
w.setVisibility(View.VISIBLE);
list.invalidate();
}
});
}
Here is the stacktrace:
07-03 03:17:30.335: WARN/dalvikvm(343): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): FATAL EXCEPTION: main
07-03 03:17:30.365: ERROR/AndroidRuntime(343): java.lang.NullPointerException
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1372)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.View.draw(View.java:6986)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.widget.ScrollView.draw(ScrollView.java:1409)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.View.draw(View.java:6883)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.View.draw(View.java:6883)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewRoot.draw(ViewRoot.java:1522)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.os.Looper.loop(Looper.java:123)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at java.lang.reflect.Method.invoke(Method.java:507)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): at dalvik.system.NativeStart.main(Native Method)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也有同样的问题。您必须使用处理程序来执行修改 UI 的代码(在本例中为removeView())。我将添加一个 Handler 字段并在 onCreate() 方法中构造它。然后,在 onAnimationEnd() 中,执行以下操作:
I had the same problem. You have to use a Handler to execute the code that modifies the UI (removeView() in this case). I would add a Handler field and construct it in your onCreate() method. Then, in onAnimationEnd(), just do this:
要删除布局的所有子级,然后添加新的子级,请找到以下代码
To remove all child of layout and then add new childs, find below code