0

我有一个带有 TimePicker 的 Activity,一个带有 aliments 列表的 Spinner 和一个按钮。当我按下按钮时,我想动态地将 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.
4

1 回答 1

1

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

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;
}
于 2013-11-06T04:48:54.770 回答