【发布时间】:2018-06-05 01:13:18
【问题描述】:
当我尝试将行动态添加到我的 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}:'java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。 在 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 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 数据库来存储和显示时间表的应用程序。我按照从网上找到的方法在表格中显示结果。如果您可以为此代码 sn-p 建议更有效的方法,将对我有所帮助。
【问题讨论】:
-
'java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.不是很清楚吗?!
标签: android dynamic row tablelayout