0

运行时添加视图的代码如下:contactActivity.java:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_contact_details);

        mPhoneList = (LinearLayout) findViewById(R.id.contact_phone_list);

        View view = getLayoutInflater().inflate(R.layout.contact_details_item, mPhoneList,false);
        mItemLabel = (TextView) view.findViewById(R.id.contact_item_label);
        mItemValue = (TextView) view.findViewById(R.id.contact_item_value);



        //mContactItem = (LinearLayout) findViewById(R.id.contact_item);

        mItemLabel.setText("home");  //NULL POINTER EXCEPTION HERE
        mItemValue.setText("999999999999");

        mPhoneList.addView(view);

        mItemLabel.setText("work");
        mItemValue.setText("999gggggggggg");
        mPhoneList.addView(view);   


    }

contact_phone_list 在activity_contact_details.xml 中:

some code...
<LinearLayout
                android:id="@+id/contact_phone_list"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:background="@color/silver"
                android:orientation="horizontal"
                android:padding="16dp" >

            </LinearLayout>
some more code...

contact_details_item.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contact_item"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView 
        android:id="@+id/contact_item_label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:textColor="@color/orange"
        android:gravity="right"/>

    <TextView 
        android:id="@+id/contact_item_value"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.7"
        android:textStyle="bold"
        android:gravity="left"/>

</LinearLayout>

日志猫:

09-18 19:18:22.359: E/AndroidRuntime(28218):    at dalvik.system.NativeStart.main(Native Method)
09-18 19:18:22.359: E/AndroidRuntime(28218): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.view.ViewGroup.addView(ViewGroup.java:3249)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.view.ViewGroup.addView(ViewGroup.java:3194)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.view.ViewGroup.addView(ViewGroup.java:3170)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at com.glad2.ui.ContactDetailsActivity.onCreate(ContactDetailsActivity.java:51)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.app.Activity.performCreate(Activity.java:5008)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-18 19:18:22.359: E/AndroidRuntime(28218):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-18 19:18:22.359: E/AndroidRuntime(28218):    ... 11 more 

有人可以帮我理解崩溃的原因吗?

修改代码(现在我得到一个没有数据的空白屏幕):

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_contact_details);

        mPhoneList = (LinearLayout) findViewById(R.id.contact_phone_list);
        mEmailList = (LinearLayout) findViewById(R.id.contact_email_list);

        for (int i=0; i<5;i++){
            View view = getLayoutInflater().inflate(R.layout.contact_details_item, mPhoneList,false);
            mItemLabel = (TextView) view.findViewById(R.id.contact_item_label);
            mItemValue = (TextView) view.findViewById(R.id.contact_item_value);


            //mContactItem = (LinearLayout) findViewById(R.id.contact_item);
            String t[] = new String[] {"abc", "def", "df", "sd", "kg", "lk"}; 
            mItemLabel.setText(t[i]);
            mItemValue.setText(String.valueOf(i));

            mPhoneList.addView(view);

        }

谢谢和问候,阳光

4

3 回答 3

1

您需要更改这些行

mItemLabel = (TextView) findViewById(R.id.contact_item_label);
mItemValue = (TextView) findViewById(R.id.contact_item_value);

mItemLabel = (TextView) view.findViewById(R.id.contact_item_label);
mItemValue = (TextView) view.findViewById(R.id.contact_item_value);

按照目前的编写方式,您正在寻找activity_contact_details.xmlfor these TextViews,但您需要在contact_details_item.xmlfor the ids 中寻找,因为那是他们居住的地方。

如果这不能解决您的问题,请发布 logcat。

编辑

在您的 xml 中将“home”和“work” LinearLayouts 包裹在根目录中,LinearLayout然后它们都将在您的文件中view并且只添加view一次mPhoneListLinearLayout如果您有未确定数量的“项目”(例如移动设备、work2 等),您可以稍后动态添加s

前任。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contact_item"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
   <LinearLayout
       android:id="@+id/homeLL
       // add other properties, use default horizontal orientation>
      <TextView 
          android:id="@+id/contact_item_label"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="0.3"
          android:textColor="@color/orange"
          android:gravity="right"/>

      <TextView 
          android:id="@+id/contact_item_value"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="0.7"
          android:textStyle="bold"
          android:gravity="left"/>
     </LinearLayout
     <LinearLayout
         android:id="@+id/workLL
        // add other properties>
       //add your TextViews as in your first LL
     </LinearLayout>
</LinearLayout>

动态创建视图

动态创建线性布局

将 TextViews 添加到 LinearLayout

于 2013-09-18T13:43:13.200 回答
0

你应该把:

   mItemLabel = (TextView) view.findViewById(R.id.contact_item_label);
    mItemValue = (TextView) view.findViewById(R.id.contact_item_value);

代替 :

 mItemLabel = (TextView) findViewById(R.id.contact_item_label);
    mItemValue = (TextView) findViewById(R.id.contact_item_value);

它应该可以正常工作

于 2013-09-18T13:44:40.890 回答
0

尝试这个:

View view = getLayoutInflater().inflate(R.layout.contact_details_item, mPhoneList,false);
    mItemLabel = (TextView) view.findViewById(R.id.contact_item_label);
    mItemValue = (TextView) view.findViewById(R.id.contact_item_value);

你应该在你的膨胀视图中搜索contact_item_label和。contact_item_value不在活动的内容布局中。

于 2013-09-18T13:43:04.427 回答