【发布时间】:2015-04-01 08:14:07
【问题描述】:
我尝试在相对布局中创建滚动视图以使表格布局可滚动,但我不断收到此错误:
02-02 19:29:10.116: E/AndroidRuntime(9400):
java.lang.RuntimeException: Unable to start activity ComponentInfo{test.com.classmanagertest/test.com.classmanagertest.StudentsMasterList}: java.lang.IllegalStateException:
The specified child already has a parent. You must call removeView() on the child's parent first.
这是我在数据库中显示字段的表格布局:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.students_masterlist);
db=openOrCreateDatabase("ClassManager",MODE_WORLD_WRITEABLE, null);
Cursor c=db.rawQuery("SELECT StudPic, StudentID, LastName FROM MasterStudents", null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView, textView1;
ImageView StudImageView;
RelativeLayout rl=(RelativeLayout)findViewById(R.id.layout);
ScrollView sv = new ScrollView(this);
sv.addView(tableLayout);
rl.addView(sv);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
StudImageView = new ImageView(getApplicationContext());
StudImageView.setPadding(20, 20, 20, 20);
StudImage=c.getBlob(c.getColumnIndex("StudPic"));
Bitmap b1= BitmapFactory.decodeByteArray(StudImage, 0, StudImage.length);
StudImageView.setImageBitmap(b1);
tableRow.addView(StudImageView);
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("StudentID")));
textView1.setPadding(20, 20, 20, 20);
textView1.setTextColor(getResources().getColor(R.color.blueactionbar));
textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView1.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView1);
textView = new TextView(getApplicationContext());
textView.setText(c.getString(c.getColumnIndex("LastName")));
textView.setPadding(20, 20, 20, 20);
textView.setTextColor(getResources().getColor(R.color.blueactionbar));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);
textView.setTypeface(null, Typeface.BOLD);
tableRow.addView(textView);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
setContentView(tableLayout);
db.close();
}
这是我的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:scrollbars="vertical|horizontal"
android:id="@+id/layout">
</RelativeLayout>
如何解决此错误?
任何帮助表示赞赏!非常感谢!
【问题讨论】:
-
为什么要设置两次内容视图?而您的表格视图是滚动视图中的子视图(这是相对布局的子视图)。当您第二次设置内容视图时也是如此。这也是在活动上完成的。这是一种错误的编码方式
-
所以我删除了第一个 setcontentview?
-
是的,您需要刷新整体视图。要做到这一点,一旦你添加了你调用的所有行
requestLayout() -
你能写代码吗?所以我能更好地理解它吗?我是处理 Android Studio 的新手。
-
它不是 android studio 问题 :).. 好的将添加代码。
标签: java android tablelayout