我试图以编程方式创建TextView并使用addView()函数将其添加到我的布局中,但是我得到了运行时异常,即说明“指定的子节点已经有父节点。您必须调用removeView()孩子的父母第一次“。我查看了有关此主题的所有问题,但仍然无法理解如何以及何时使用removeView()函数。
所以这是我的代码:
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private int maxNumber = 6;
private Integer[] numbers = new Integer[maxNumber];
private Integer[] textSizes = new Integer[maxNumber];
private Integer[] colors = new Integer[8];
private Integer[] randomColors = new Integer[maxNumber];
private Integer[][] margins;
private LayoutParams params[];
private int maxValue = 1000;
private int maxSize = 50;
private int minSize = 15;
private int viewCount = 0;
private int startCount = 3;
private int layoutHeight;
private int layoutWidth;
private int score = 0;
private RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (RelativeLayout) findViewById(R.id.layout);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
layoutHeight = displayMetrics.heightPixels;
layoutWidth = displayMetrics.widthPixels;
String[] colorsTxt = getApplicationContext().getResources().getStringArray(R.array.colors);
for (int i = 0; i < colorsTxt.length; i++) {
int newColor = Color.parseColor(colorsTxt[i]);
colors[i] = newColor;
}
createTextView(3);
}
private void createTextView(int num)
{
final TextView tv = new TextView(this);
createProperties(num);
eliminateSameRandoms();
for(int i = viewCount; i < (viewCount + num); i++)
{
tv.setLayoutParams(params[i]);
tv.setText(String.valueOf(numbers[i]));
tv.setTextColor(randomColors[i]);
tv.setTextSize(textSizes[i]);
createListener(tv);
layout.addView(tv);
}
viewCount += num;
}
private void createListener(final TextView textView)
{
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Integer.parseInt(String.valueOf(textView.getText())) == getBiggest()) {
score++;
if(viewCount == startCount * startCount) {
startCount++;
createTextView(startCount);
}
else {
createTextView(startCount);
}
} else {
Intent i = new Intent(MainActivity.this, FailActivity.class);
i.putExtra("score", score);
startActivity(i);
}
}
});
}
private int getBiggest()
{
int max = 0;
for(int i : numbers)
{
if(i > max)
{
max = i;
}
}
return max;
}
private void createProperties(int num)
{
createRandoms(num);
createColors(num);
createTextSizes(num);
createMargins(num);
createParams(num);
}
private void createRandoms(int numRandoms)
{
for(int i = viewCount; i < (viewCount + numRandoms); i++)
{
numbers[i] = new Random().nextInt(maxValue);
}
}
private void createParams(int numParams)
{
params = new LayoutParams[numParams];
for(int i = viewCount; i < (viewCount + numParams); i++)
{
params[i] = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params[i].leftMargin = margins[i][0];
params[i].topMargin = margins[i][1];
}
}
private void createMargins(int numMargins)
{
margins = new Integer[numMargins][2];
for(int i = viewCount; i < (viewCount + numMargins); i++)
{
margins[i][0] = new Random().nextInt(layoutWidth - 40) + 20;
margins[i][1] = new Random().nextInt(layoutHeight - 40) + 20;
}
}
private void createTextSizes(int numTextSizes)
{
for(int i = viewCount; i < (viewCount + numTextSizes); i++)
{
textSizes[i] = new Random().nextInt(maxSize - minSize) + minSize;
}
}
private void createColors(int numColors)
{
for(int i = viewCount; i < (viewCount + numColors); i++)
{
randomColors[i] = colors[new Random().nextInt(colors.length - 1)];
}
}
private boolean inRange(int position, int other)
{
int range = 60;
return !(position - other < range && other - position < range);
}
private void eliminateSameRandoms()
{
int numRandoms = viewCount;
int i = 0;
while(i < numRandoms)
{
for(int j = 0; j < numRandoms; j++)
{
Log.d("i: " , String.valueOf(i));
Log.d("j: " , String.valueOf(j));
if(j != (numRandoms - i))
{
while (numbers[j].equals(numbers[numRandoms - (i + 1)]))
{
numbers[j] = new Random().nextInt(maxValue);
}
while (randomColors[j].equals(randomColors[numRandoms - (i + 1)]))
{
randomColors[j] = colors[new Random().nextInt(colors.length)];
}
while (textSizes[j].equals(textSizes[numRandoms - (i + 1)]))
{
textSizes[j] = new Random().nextInt(maxSize - minSize) + minSize;
}
while(inRange(margins[j][0], margins[numRandoms - (i + 1)][0]))
{
margins[j][0] = new Random().nextInt(layoutWidth - 40) + 20;
}
while(inRange(margins[j][1], margins[numRandoms - (i + 1)][1]))
{
margins[j][1] = new Random().nextInt(layoutHeight - 40) + 20;
}
}
}
i++;
}
}
}
我的XML布局中只有一个RelativeLayout,其id = layout
我尝试使用
((ViewGroup)tv.getParent()).removeView(tv);
前
layout.addView(tv);
在我的代码中的各个位置,但没有一个工作。
我应该在哪里以及如何使用removeView()?
答案 0 :(得分:0)
您一遍又一遍地向您的布局添加相同的TextView
。第一次之后,TextView
有一个父级,这就是你得到错误的原因。在循环中初始化TextView
,它将修复您的错误:
for(int i = viewCount; i < (viewCount + num); i++)
{
final TextView tv = new TextView(this);
tv.setLayoutParams(params[i]);
tv.setText(String.valueOf(numbers[i]));
tv.setTextColor(randomColors[i]);
tv.setTextSize(textSizes[i]);
createListener(tv);
layout.addView(tv);
}
如果您像这样初始化它,则每次都创建一个新的TextView
,但是还没有父级。