我是android的新手,我正试图在另一个下面动态添加1个布局。我已经看过类似的问题&之前的答案,但我似乎找不到自己的方式。我希望有人可以为我清理事情。
我想要添加第二个布局的主要布局(import_custom_fields):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/import_custom_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="6dp">
<FrameLayout
android:clickable="true"
android:id="@+id/profile_picture_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:foreground="@drawable/selector">
<ImageView
android:id="@+id/ProfilePicture"
android:layout_width="@dimen/profile_picture_size"
android:layout_height="@dimen/profile_picture_size"
android:scaleType="centerCrop"
android:contentDescription="@string/menu_profile_picture_header"
android:src="@drawable/ic_contact_picture" />
<ImageView
android:id="@+id/addPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/menu_profile_picture_add"
android:layout_gravity="bottom|right"
android:src="@drawable/plus" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="@+id/profile_picture_frame"
android:layout_centerVertical="true">
<EditText
android:id="@+id/FirstNameEdit"
android:tag="@string/vcard_tag_first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textAppearance="@android:style/TextAppearance.Large"
android:hint="@string/first_name"
android:inputType="textPersonName|textCapWords"
android:lines="1"/>
<EditText
android:id="@+id/LastNameEdit"
android:tag="@string/vcard_tag_last_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textAppearance="@android:style/TextAppearance.Large"
android:hint="@string/last_name"
android:inputType="textPersonName|textCapWords"
android:lines="1" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
第二个布局(fields_to_inflate):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fields_to_inflate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spinnerPhone"
android:layout_width="100dip"
android:layout_marginTop="10dip"
android:layout_marginLeft="10dip"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/spinnerPhone"
android:layout_toRightOf="@+id/spinnerPhone"
android:inputType="phone"
android:ems="10" >
</EditText>
</RelativeLayout>
最后,我的活动如下:
public class ImportCustomFields extends SherlockActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.import_custom_fields);
Spinner spinner = (Spinner)findViewById(R.id.spinnerPhone);
ImageView addField = (ImageView)findViewById(R.id.add);
addField.setClickable(true);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.custom_fields,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final LinearLayout mainLayout = (LinearLayout)findViewById(R.layout.import_custom_fields);
final RelativeLayout inflateLayout = (RelativeLayout)View.inflate(this, R.layout.fields_to_inflate, null);
addField.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mainLayout.addView(inflateLayout);
}
});
}
}
我在此行(mainLayout.addView(inflateLayout))获得null异常错误。
答案 0 :(得分:2)
您应该使用您指定的布局的ID:
final LinearLayout mainLayout = (LinearLayout)findViewById(R.id.import_custom_field);