0

私はAndroidを初めて使用し、1つのレイアウトを別のレイアウトの下に動的に追加しようとしています。私は以前に同様の質問と回答を見たことがありますが、自分の道を見つけることができないようです。誰かが私のために物事を片付けることができることを願っています。

2番目のレイアウトを追加したいメインレイアウト(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>

2番目のレイアウト(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例外エラーが発生します。

4

1 に答える 1

2

割り当てたレイアウトの ID を使用する必要があります。

final LinearLayout mainLayout = (LinearLayout)findViewById(R.id.import_custom_field);
于 2013-01-31T17:52:42.470 に答える