当我尝试动态地向我的tableview添加行时发生上述错误 主要活动代码。我已经尝试了很多堆栈溢出的解决方案。但没有解决我的问题。
package com.example.bhramaram.myapplication;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.example.bhramaram.myapplication.Dbhelper;
import com.example.bhramaram.myapplication.R;
public class jav extends AppCompatActivity {
TableLayout tb;
TableRow tableRow;
Dbhelper dbhelper;
@Override
public void onCreate(Bundle savedInstanceState){
int day=0;
super.onCreate(savedInstanceState);
setContentView(R.layout.timetable);
getvalues();
}
private void getvalues() {
tb=(TableLayout)findViewById(R.id.timetble);
TextView textView1,textView2,textView3,textView4,textView5,textView0;
textView0=new TextView(this);textView1=new TextView(this);
textView2=new TextView(this);textView3=new TextView(this);
textView4=new TextView(this);textView5=new TextView(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
textView0.setLayoutParams(lp);textView1.setLayoutParams(lp);
textView2.setLayoutParams(lp);textView3.setLayoutParams(lp);
textView4.setLayoutParams(lp);textView5.setLayoutParams(lp);
dbhelper= new Dbhelper(jav.this);
Cursor cursor= dbhelper.recieveData();
if(cursor.getCount()==0){
alert("Nothing","Nothing to show");
return;}
int i=0;
while (cursor.moveToNext())
{
tableRow=new TableRow(this);
TableLayout.LayoutParams lp1=new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);
textView0.setText((cursor.getString(0)));textView1.setText((cursor.getString(1)));
textView2.setText((cursor.getString(2)));textView3.setText((cursor.getString(3)));
textView4.setText((cursor.getString(4)));textView5.setText((cursor.getString(5)));
tableRow.setLayoutParams(lp);
//Error occurred here
tableRow.addView(textView0);tableRow.addView(textView1);
tableRow.addView(textView2);tableRow.addView(textView3);
tableRow.addView(textView4);tableRow.addView(textView5);
tb.addView(tableRow,lp1);
}
}
private void alert(String title, String alert) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(alert);
}
}
相应的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/timetble"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
tools:ignore="MissingConstraints">
</TableLayout>
</android.support.constraint.ConstraintLayout>
错误详情
处理:com.example.bhramaram.myapplication,PID:14266 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.bhramaram.myapplication / com.example.bhramaram.myapplication.jav}:&#39; java.lang.IllegalStateException:指定的子项已经有父项。您必须先在孩子的父母身上调用removeView()。&#39; 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 引起:java.lang.IllegalStateException:指定的子节点已经有父节点。您必须首先在孩子的父母上调用removeView()。 在android.view.ViewGroup.addViewInner(ViewGroup.java:4460) 在android.view.ViewGroup.addView(ViewGroup.java:4301) 在android.view.ViewGroup.addView(ViewGroup.java:4241) 在android.view.ViewGroup.addView(ViewGroup.java:4214) 在com.example.bhramaram.myapplication.jav.getvalues(jav.java:60) 在com.example.bhramaram.myapplication.jav.onCreate(jav.java:29) 在android.app.Activity.performCreate(Activity.java:6705) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
为什么我收到此错误?
此外,我是Android开发者社区的新手。这个项目是关于一个使用本地SQLite数据库来存储和显示时间表的应用程序。我按照我在网上找到的方法在表格中显示结果。如果您可以为此代码段建议更有效的方法,对我有帮助。
答案 0 :(得分:2)
在这里,您可以创建文本视图:
TextView textView1,textView2,textView3,textView4,textView5,textView0;
textView0=new TextView(this);textView1=new TextView(this);
textView2=new TextView(this);textView3=new TextView(this);
textView4=new TextView(this);textView5=new TextView(this);
然后,在循环内部,您将相同的TextView
多次添加到TableRow
中:
tableRow.addView(textView0);tableRow.addView(textView1);
tableRow.addView(textView2);tableRow.addView(textView3);
tableRow.addView(textView4);tableRow.addView(textView5);
tb.addView(tableRow,lp1);
由于之前已将TextView
添加到一行,因此它们已经有父视图并导致您提到的错误。
为了解决问题,您必须将new TextView()
部分移动到循环中。您必须为每个循环迭代创建新对象:
while (cursor.moveToNext()) {
tableRow=new TableRow(this);
TableLayout.LayoutParams lp1=new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);
// Create new objects before adding them
textView0=new TextView(this);textView1=new TextView(this);
textView2=new TextView(this);textView3=new TextView(this);
textView4=new TextView(this);textView5=new TextView(this);
textView0.setText((cursor.getString(0)));textView1.setText((cursor.getString(1)));
textView2.setText((cursor.getString(2)));textView3.setText((cursor.getString(3)));
textView4.setText((cursor.getString(4)));textView5.setText((cursor.getString(5)));
tableRow.setLayoutParams(lp);
tableRow.addView(textView0);tableRow.addView(textView1);
tableRow.addView(textView2);tableRow.addView(textView3);
tableRow.addView(textView4);tableRow.addView(textView5);
tb.addView(tableRow,lp1);
}