2016-07-31 72 views
-4

我收到以下錯誤 -java.lang.IllegalStateException:指定的子項已具有父項。如何處理錯誤

java.lang.IllegalStateException:指定的子項已具有父項。您必須先調用子對象的父對象的removeView()。

我是新來的android編程,我不知道什麼inflater是。這是我的代碼產生這個錯誤信息。

package com.example.android.miwok; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.text.Layout; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 

public class NumbersActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_numbers); // gives Null Pointer Exception if this line is removed. 

     List<String> numbers = new ArrayList<String>(); 
     numbers.add("one"); 
     numbers.add("two"); 
     numbers.add("three"); 
     numbers.add("four"); 
     numbers.add("five"); 
     numbers.add("six"); 
     numbers.add("seven"); 
     numbers.add("eight"); 
     numbers.add("nine"); 
     numbers.add("ten"); 

     LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView); 
     TextView numberView = new TextView(this); 

     for (int i = 0; i < 10; i++) { 
      numberView.setText(numbers.get(i)); 
      // adds a Text View to the Linear Layout (adding child to the parent) 
      rootView.addView(numberView); 
     } 
    } 
} 

這是

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.android.miwok.PhraseActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay"/> 

    </android.support.design.widget.AppBarLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:id="@+id/rootView"> 
    </LinearLayout> 

</android.support.design.widget.CoordinatorLayout> 

我想在屏幕上顯示的號碼activity_numbers.xml文件。

謝謝你的幫助。

+4

歡迎來到SO。請訪問:http://stackoverflow.com/help/how-to-ask閱讀如何爲您的問題提供適當的標題。 – Shaishav

+0

請寫一個總結特定問題的標題。請參閱[如何提問](http://stackoverflow.com/help/how-to-ask)瞭解更多信息。 –

+0

我爲我的錯誤表示歉意。 –

回答

0

您正在重複添加相同的視圖,因此出現錯誤。這應該修復它:

LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView); 

     for (int i = 0; i < 10; i++) { 
      TextView numberView = new TextView(this); 
      numberView.setText(numbers.get(i)); 
      // adds a Text View to the Linear Layout (adding child to the parent) 
      rootView.addView(numberView); 
     } 
相關問題