以编程方式将Textview添加到Relativelayout时,应用程序崩溃
错误消息:java.lang.RuntimeException:无法恢复活动:java.lang.IllegalStateException:android.support.v7.app.WindowDecorActionBar.getDecorToolbar中的RelativeLayout
注意: 当Activity扩展 FragmentActivity 时,它可以正常工作。但是扩展 AppcompactAcitivty 应用程序已崩溃。
代码
TextView bt = new TextView(getContext());
bt.setText(" " + id);
bt.setId(++id);
bt.getId();
bt.setBackground(getResources().getDrawable(R.drawable.annotation_circle));
// bt.setBackgroundColor(Color.BLACK);
bt.setTextColor(Color.WHITE);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// showDialog();
ToastHandler.newInstance(getContext()).mustShowToast(defString(annotations.getComment()));
}
});
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(xdevice, ydevice, 0, 0);
bt.setLayoutParams(params);
if(bt.getParent()!=null)
((ViewGroup)bt.getParent()).removeView(bt);
if( paint_rlay.getChildCount() <= 0)
{
if(bt.getParent()!=null)
((ViewGroup)bt.getParent()).removeView(bt);
paint_rlay.addView(bt);
}
else
{
for (int i = 0; i < paint_rlay.getChildCount(); i++)
{
if (!checkCollision(bt, paint_rlay.getChildAt(i)))
{
if(bt != paint_rlay.getChildAt(i))
{
if(bt.getParent()!=null)
((ViewGroup)bt.getParent()).removeView(bt);
paint_rlay.addView(bt);
break;
}
}
}
}
答案 0 :(得分:0)
在主题中将windowActionBar设置为false,以改为使用工具栏。
并且不要忘记在主题中设置以下属性。
<item name="windowActionBar">false</item>
答案 1 :(得分:0)
更改您的
TextView bt = new TextView(getApplicationContext());
与
[Vue warn]: Cannot find element: #app
如果你想在扩展AppCompatActivity的活动中使用actionbar,那么使用工具栏而不是动作栏来使用它。