我有一个带有TimePicker的Activity,一个带有食品清单和一个按钮的Spinner。当我按下按钮时,我想向视图中动态添加微调器。

这是我的按钮:

<Button
    android:layout_marginTop="350dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Aggiungi un altro alimento"
    android:id="@+id/button"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:onClick="addAnotherAliment"/>


这是onClick事件后调用的相关方法

public void addAnotherAliment(View view) {
    Spinner alimentSpinner = new Spinner(this);
    ArrayAdapter<String> alimentAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_spinner_dropdown_item,
            getResources().getStringArray(R.array.aliments)
    );
    alimentSpinner.setAdapter(alimentAdapter);
    LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container);
    containerLayout.addView(alimentSpinner);
}


但我收到“很遗憾,您的应用已停止”。怎么了?

这是整个活动:

public class SecondActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        populateFirstSpinner();
    }

    private void populateFirstSpinner() {
        setContentView(R.layout.activity_second);
        Spinner spinnerAliments = (Spinner) findViewById(R.id.alimenti);
        ArrayAdapter<String> spinnerCountAliments = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.aliments));
        spinnerAliments.setAdapter(spinnerCountAliments);
    }

    public void addAnotherAliment(View view) {
        Spinner alimentSpinner = new Spinner(this);
        ArrayAdapter<String> alimentAdapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_spinner_dropdown_item,
                getResources().getStringArray(R.array.aliments)
        );
        alimentSpinner.setAdapter(alimentAdapter);
        LinearLayout containerLayout = (LinearLayout)findViewById(R.id.container_registration);
        containerLayout.addView(alimentSpinner);
    }


}


Logcat说:

11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 0 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 1 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 2 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 3 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 4 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 5 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 6 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 7 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 8 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 9 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 10 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 11 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 12 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 13 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 14 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 15 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 16 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 17 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 18 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 19 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 20 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 21 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 22 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 23 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 24 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 25 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 26 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 27 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 28 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 29 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 30 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 31 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 32 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.
11-05 18:05:21.002    1113-1113/com.android.defcontainer W/dalvikvm﹕ PR_CAPBSET_DROP 33 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled.

最佳答案

核实。编写程序中所需的线性或任何其他布局。

public Spinner addNewSpinner(int id, String[] vals) {

    sp = new Spinner(this);
    sp.setId(id);


    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, vals);
    adapter.setDropDownViewResource((R.layout.multiline_spinner_dropdown_item));
    sp.setAdapter(adapter);

    linearLayout.addView(sp);

    return sp;
}

09-09 20:17