Spinner无法在弹出窗口中工作 - Android

时间:2013-07-20 17:23:16

标签: android popup android-spinner

我有一个弹出窗口,要求您输入一些信息,然后假设选择一个spinner的状态。我可以将spinner放在xml中并显示正常,但是当我尝试激活spinner时,它会崩溃应用。以下是我用来创建spinner

的代码
public class DashboardActivity extends ListActivity implements OnItemSelectedListener {
    private TextView state_selection;
    private static final String[] items = {
        "Select State of Driver",
            "Alabama",
            "Alaska",
            "Arizona",
            "Arkansas",
            "California",
            "Colorado",
            "Connecticut",
            "Delaware",
            "District of Columbia",
            "Florida",
            "Georgia",
            "Hawaii",
            "Idaho",
            "Illinois",
            "Indiana",
            "Iowa",
            "Kansas",
            "Kentucky",
            "Louisiana",
            "Maine",
            "Maryland",
            "Massachusetts",
            "Michigan",
            "Minnesota",
            "Mississippi",
            "Missouri",
            "Montana",
            "Nebraska",
            "Nevada",
            "New Hampshire",
            "New Jersey",
            "New Mexico",
            "New York",
            "North Carolina",
            "North Dakota",
            "Ohio",
            "Oklahoma",
            "Oregon",
            "Pennsylvania",
            "Puerto Rico",
            "Rhode Island",
            "South Carolina",
            "South Dakota",
            "Tennessee",
            "Texas",
            "Utah",
            "Vermont",
            "Virginia",
            "Washington",
            "West Virginia",
            "Wisconsin",
            "Wyoming"
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xmlfile);

        state_selection = (TextView) findViewById(R.id.state_selection);
        Spinner my_spin = (Spinner) findViewById(R.id.state_spinner);
        my_spin.setOnItemSelectedListener(this);
        ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, items);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        my_spin.setAdapter(aa);
    }

    @Override
    public void onItemSelected(AdapterView arg0, View arg1, int pos, long arg3) {
        state_selection.setText(items[pos]);
    }

    @Override
    public void onNothingSelected(AdapterView arg0) {
        // TODO Auto-generated method stub
        state_selection.setText("");
    }
}

这是我的Logcat

07-20 13:28:25.535: E/AndroidRuntime(1378): FATAL EXCEPTION: main
07-20 13:28:25.535: E/AndroidRuntime(1378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.DashboardActivity}: java.lang.NullPointerException
07-20 13:28:25.535: E/AndroidRuntime(1378):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at android.os.Looper.loop(Looper.java:154)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at android.app.ActivityThread.main(ActivityThread.java:4945)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at java.lang.reflect.Method.invokeNative(Native Method)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at java.lang.reflect.Method.invoke(Method.java:511)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at dalvik.system.NativeStart.main(Native Method)
07-20 13:28:25.535: E/AndroidRuntime(1378): Caused by: java.lang.NullPointerException
07-20 13:28:25.535: E/AndroidRuntime(1378):     at com.example.DashboardActivity.onCreate(DashboardActivity.java:160)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at android.app.Activity.performCreate(Activity.java:4531)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
07-20 13:28:25.535: E/AndroidRuntime(1378):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)

2 个答案:

答案 0 :(得分:0)

  

07-20 13:28:25.535:E / AndroidRuntime(1378):引起:   显示java.lang.NullPointerException

你忘了添加

setContentView(R.layout.layout_name);之后

super.onCreate(savedInstanceState);

答案 1 :(得分:0)

检查您的

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

}