在运行时添加视图的代码如下: 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>
logcat的:
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);
}
谢谢和问候, 晴天
答案 0 :(得分: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.xml
的{{1}},但您需要查看TextView
以查找contact_details_item.xml
这就是他们居住的地方。
如果这不能解决您的问题,请发布logcat。
修改强>
将xml中的“home”和“work”id
包裹在根目录LinearLayout
中,然后这些都会在LinearLayout
中,并且只会添加view
view
一次。如果您有不确定数量的“项目”(例如,移动,工作2等等),您可以稍后动态添加mPhoneList
。
实施例
LinearLayout
动态创建视图
答案 1 :(得分: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
。不在活动的内容布局中。
答案 2 :(得分: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);
它应该可以正常工作