Der obige Fehler trat auf, während ich versuche, Zeile dynamisch zu meinem Tableview Main-Code der Aktivität hinzuzufügen. Ich habe viele Lösungen im Stapelüberlauf versucht. Aber nichts hat mein Problem gelöst.Fehler empfangen "Sie müssen RemoveView() zuerst auf dem übergeordneten Element des Kindes aufrufen"
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);
}
}
entsprechenden Layout
<?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>
Fehlerdetails
Prozess: com.example.bhramaram.myapplication, PID: 14266 java.lang.RuntimeException: Kann Aktivität ComponentInfo starten {com.example.bhramaram.myapplication.jav}: 'java.lang.IllegalStateException: Das angegebene Kind hat bereits einen Elternteil. Sie müssen zuerst removeView() für das übergeordnete Element des Kindes aufrufen. bei android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2646) bei android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2707) bei android.app.ActivityThread.-wrap12 (ActivityThread.java) bei Android .app.ActivityThread $ H.handleMessage (ActivityThread.java:1460) bei android.os.Handler.dispatchMessage (Handler.java:102) bei android.os.Looper.loop (Looper.java:154) bei Android .app.ActivityThread.main (ActivityThread.java:6077) bei java.lang.reflect.Method.invoke (systemeigene Methode) bei com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:865) bei com.android.internal.os.ZygoteInit.mai n (ZygoteInit.java:755) verursacht durch: java.lang.IllegalStateException: Das angegebene Kind hat bereits einen Elternteil. Sie müssen zuerst removeView() für das übergeordnete Element des Kindes aufrufen. bei android.view.ViewGroup.addViewInner (ViewGroup.java:4460) bei android.view.ViewGroup.addView (ViewGroup.java:4301) bei android.view.ViewGroup.addView (ViewGroup.java:4241) bei android.view.ViewGroup.addView (ViewGroup.java:4214) bei com.example.bhramaram.myapplication.jav.getvalues (jav.java:60) bei com.example.bhramaram.myapplication.jav.onCreate (jav. Java: 29) bei android.app.Activity.performCreate (Activity.java:6705) bei android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1119) bei android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2599)
Warum erhalte ich diesen Fehler?
Auch ich bin ein Neuling in der Android-Entwickler-Community. Bei diesem Projekt handelt es sich um eine App, die lokale SQLite-Datenbanken zum Speichern und Anzeigen der Zeittabelle verwendet. Ich folge der Methode, die ich aus dem Internet gefunden habe, um die Ergebnisse in Tabelle zu zeigen. Es wird hilfreich für mich sein, wenn Sie effizientere Methoden für dieses Code-Snippet vorschlagen können.
''java.lang.IllegalStateException: Das angegebene Kind hat bereits einen Elternteil. Sie müssen removeView() zuerst beim Elternteil des Kindes aufrufen. Ist das nicht klar ?! –