我已经以编程方式将CheckedTextView添加到了线性布局视图中。请看以下代码:
private LinearLayout linearLayout;
private CheckedTextView checkedtextview;
linearLayout = (LinearLayout) findViewById(R.id.statusView);
checkedtextview = new CheckedTextView(ScanStatus.this, null, android.R.attr.listChoiceIndicatorMultiple);
checkedtextview.setText(R.string.applications);
linearLayout.addView(checkedtextview);稍后在代码中,我必须更新checkedtextview,如下所示:
checkedtextview.setCheckMarkDrawable(getDrawable(R.mipmap.check1));
checkedtextview.setChecked(true);
checkedtextview.setTextColor(Color.GREEN);
linearLayout.addView(checkedtextview);但这会导致崩溃,日志如下:
D/AndroidRuntime( 24818 ):关闭VM E/AndroidRuntime(24818):致命异常: main E/AndroidRuntime(24818):Process: com.example.ashwini.timapp,PID: 24818 E/AndroidRuntime(24818):java.lang.IllegalStateException:指定的子进程已有父进程。您必须首先对子对象的父级调用removeView()。
请建议我如何更新视图?
发布于 2017-03-13 14:11:15
您有两个选择。首先,如果你一直都有一个对你的checkedtextview视图的引用--你可以在不调用addView的情况下更新它:
checkedtextview.setCheckMarkDrawable(getDrawable(R.mipmap.check1));
checkedtextview.setChecked(true);
checkedtextview.setTextColor(Color.GREEN);. 在第二种情况下,使用@坚持远方answer提供的提示:
linearLayout.removeView(checkedtextview);
checkedtextview.setCheckMarkDrawable(getDrawable(R.mipmap.check1));
checkedtextview.setChecked(true);
checkedtextview.setTextColor(Color.GREEN);
linearLayout.addView(checkedtextview);发布于 2017-03-13 14:04:08
我认为首先你需要移除视图,然后再更新它。
linearLayout.removeView(checkedtextview);
checkedtextview.setCheckMarkDrawable(getDrawable(R.mipmap.check1));
checkedtextview.setChecked(true);
checkedtextview.setTextColor(Color.GREEN);
linearLayout.addView(checkedtextview);发布于 2017-03-13 14:06:08
您似乎正在尝试添加两次checkedtextview。
要更改选中状态,可以从linearLayout获取视图,如下所示
在您的类中保留checkedtextview的引用或linearLayout.getChildAt(position),并随时根据需要更改状态。
https://stackoverflow.com/questions/42757393
复制相似问题