0

我有一个弹出窗口,要求您输入一些信息,然后假设选择一个带有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)
4

2 回答 2

0

检查你的

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

}
于 2013-07-20T17:35:44.550 回答
0

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

你忘了添加

setContentView(R.layout.layout_name);之后super.onCreate(savedInstanceState);

于 2013-07-20T17:32:44.837 回答