一切正常,但我无法添加Scrollview,因为我在scrollView.addView(mainlinearLayout)中收到错误;
有类似的问题,但我没有找到答案。如果你帮助我,我会很高兴的。感谢
这是错误
java.lang.IllegalStateException:指定的子节点已经有了 家长。您必须先在孩子的父母身上调用removeView()。
这是activity_main.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinLay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
这是MainActivity的代码
public void drawer() {
String[] word=s.split(" ");
scrollView=new ScrollView(this);
mainlinearLayout = (LinearLayout) findViewById(R.id.LinLay);
mainlinearLayout.setVerticalScrollBarEnabled(true);
scrollView.addView(mainlinearLayout);
childLinearLayout = getLinearLayout();
for (int i = 1; i < word.length; i++) {
final TextView textView = new TextView(this);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setTextSize(17);
textView.setPadding(5, 5, 5, 5);
textView.setText(word[i-1].toString());
childLinearLayout.addView(textView);
if (i % separatorNum == 0 && i != 0) {
mainlinearLayout.addView(childLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(mainlinearLayout);
childLinearLayout=getLinearLayout();
}
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_LONG).show();
}
});
}
}
答案 0 :(得分:0)
如果您将activity_main.xml作为活动的内容视图:
setContentView(R.layout.activity_main)
然后您将视图附加到活动,因此将它(以及具有id = LinLay的LinearLayout)作为父级。
如果您想让ScrollView成为Activity中的主要UI元素,那么您应该
setContentView(scrollView)
或者更好的是用XML做到这一点。 ScrollView是父级后,您可以添加LinearLayout。
答案 1 :(得分:0)
您正在布局中添加滚动视图。
如果要在滚动视图中添加布局,则始终记住您只能在滚动视图中添加一个布局。您也可以在滚动视图中添加线性布局,然后可以添加相同的布局数。
答案 2 :(得分:0)
尝试下面的代码,它将检查孩子是否有父,如果有,然后删除视图,这将帮助您。
public void drawer() {
String[] word=s.split(" ");
scrollView=new ScrollView(this);
mainlinearLayout = (LinearLayout) findViewById(R.id.LinLay);
mainlinearLayout.setVerticalScrollBarEnabled(true);
scrollView.addView(mainlinearLayout);
childLinearLayout = getLinearLayout();
for (int i = 1; i < word.length; i++) {
final TextView textView = new TextView(this);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setTextSize(17);
textView.setPadding(5, 5, 5, 5);
textView.setText(word[i-1].toString());
if (textView.getParent() != null) {
((ViewGroup) textView.getParent()).removeView(textView);
}
childLinearLayout.addView(textView);
if (i % separatorNum == 0 && i != 0) {
mainlinearLayout.addView(childLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(mainlinearLayout);
childLinearLayout=getLinearLayout();
}
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_LONG).show();
}
});
}
}
答案 3 :(得分:0)
你可以试试这个,并在调用之前尝试设置setContentView(R.layout.activity_main)
。
问题是您的LinearLayout已经附加到活动视图,您尝试添加到scrollview
的同一个布局,因此它给出了错误。
public void drawer() {
//String s = "sdfsdfsdf sdfsdfsd sdfsdf sdfs sdf sdf sdf sdfdsfsdf sfsdf ssdfdsf sdfsd";
int separatorNum = 5;
String[] word = s.split(" ");
scrollView = new ScrollView(this);
mainlinearLayout = (LinearLayout) findViewById(R.id.LinLay);
mainlinearLayout.setVerticalScrollBarEnabled(true);
mainlinearLayout.addView(scrollView);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(linearLayout);
LinearLayout childLinearLayout = getLinearLayout();
for (int i = 1; i < word.length; i++) {
final TextView textView = new TextView(this);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setTextSize(17);
textView.setPadding(5, 5, 5, 5);
textView.setText(word[i - 1].toString());
childLinearLayout.addView(textView);
if (i % separatorNum == 0 && i != 0) {
linearLayout.addView(childLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// setContentView(mainlinearLayout);
childLinearLayout = getLinearLayout();
}
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_LONG).show();
}
});
}
}
private LinearLayout getLinearLayout() {
LinearLayout childLinearLayout = new LinearLayout(this);
childLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
childLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
return childLinearLayout;
}
答案 4 :(得分:0)
您尝试添加退出布局以滚动查看其给定错误,因此请尝试将滚动视图添加到现有布局
不知道它是否符合您的要求。
试试这个方法
public void drawer() {
String[] word=s.split(" ");
ScrollView scrollView = new ScrollView(this);
mainlinearLayout_ = (LinearLayout) findViewById(R.id.LinLay);
mainlinearLayout_.removeAllViews();
scrollView.removeAllViews();
mainlinearLayout_.addView(scrollView);
LinearLayout verticalLinearLayout = new LinearLayout(this);
verticalLinearLayout.setOrientation(LinearLayout.VERTICAL);
for (int j = 0; j < 3; j++) {
LinearLayout horizontalChildLinearLayout = new LinearLayout(this);
horizontalChildLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 1; i < word.length; i++) {
final TextView textView = new TextView(this);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setTextSize(17);
textView.setPadding(5, 5, 5, 5);
textView.setText(word[i - 1].toString());
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_LONG).show();
}
});
horizontalChildLinearLayout.addView(textView);
}
verticalLinearLayout.addView(horizontalChildLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
scrollView.addView(verticalLinearLayout);
}
谢谢。