从Android中的LinearLayout中删除视图时出现异常
Posted
技术标签:
【中文标题】从Android中的LinearLayout中删除视图时出现异常【英文标题】:Exception while removing a view from a LinearLayout in Android 【发布时间】:2011-07-03 01:00:43 【问题描述】:我试图在将自定义视图从 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)
【问题讨论】:
能否请您发布完整的堆栈跟踪并指出跟踪第一行中引用的行号?谢谢! NullPointerException 通常意味着您试图在一个为空的对象上调用一个方法。在调用list.removeView(w)
之类的东西之前,您应该始终检查是否为 list != null
。
【参考方案1】:
我遇到了同样的问题。您必须使用 Handler 来执行修改 UI 的代码(在本例中为 removeView())。我会添加一个 Handler 字段并在您的 onCreate() 方法中构造它。然后,在 onAnimationEnd() 中,这样做:
fHandler.post(new Runnable()
@Override
public void run()
list.removeView(w);
list.invalidate();
【讨论】:
你能解释一下为什么吗?是否有关于此的 Android 文档的链接? @kolistivra,类似的问题:***.com/questions/5569267/…。答:“当你在animationEnd中改变视图层次时,Android会抛出异常”。但是为什么 - 我不知道。【参考方案2】:要删除布局的所有子元素,然后添加新子元素,请查找以下代码
if (linearLayout.getChildCount() > 0)
linearLayout.removeAllViewsInLayout();
linearLayout.invalidate();
【讨论】:
以上是关于从Android中的LinearLayout中删除视图时出现异常的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Android 中的 RemoteViews 中删除小部件?
如何在 LinearLayout 中删除 ImageView
如何使用android中的拖放框架从一个LinearLayout切换到另一个LinearLayout