我不能使用spinner.setAdapter(adapter);

时间:2010-10-02 08:57:40

标签: android android-widget

我有CameraList类提供Cameras列表,如“SONY”,“VIVOTEK”,“VIVOTEK 7XXX”,“PIXORD”,“TOSHIBA”,“JVC E-CON”......

我想在我的微调器中列出它们但我不能。有我的代码..

public class AddCam extends MainActivity {
    CameraList cams = new CameraList();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Toast msg = Toast.makeText(AddCam.this, getString(R.string.add_new), Toast.LENGTH_LONG);

        msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);

        String a = getString(R.string.add_new);

        TextView tv = (TextView) findViewById(R.id.head);
        tv.setText(getString(R.string.add_new));

        Spinner s = (Spinner) findViewById(R.id.spinner);

        ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(
        this, R.array.cameras, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //s.setAdapter(adapter); **** there is null poınter ex.
        setContentView(R.layout.spinnerex);
    }
}

我的camlist代码是

public class CameraList {
    private static String[] cameraModelNames = {"AXIS","AVOISYS","SONY","..."}

    public  String[] getlist()
    {
        return cameraModelNames;
    }
}

这是我的logCat

10-02 12:33:50.007: ERROR/AndroidRuntime(4624): Uncaught handler: thread main exiting due to uncaught exception
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): java.lang.RuntimeException: Unable to start activity ComponentInfo{spexco.hus.cepvizyon/spexco.hus.cepvizyon.AddCam}: java.lang.NullPointerException
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at android.os.Looper.loop(Looper.java:123)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at android.app.ActivityThread.main(ActivityThread.java:4363)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at java.lang.reflect.Method.invokeNative(Native Method)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at java.lang.reflect.Method.invoke(Method.java:521)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at dalvik.system.NativeStart.main(Native Method)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624): Caused by: java.lang.NullPointerException
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at spexco.hus.cepvizyon.AddCam.onCreate(AddCam.java:39)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
10-02 12:33:50.027: ERROR/AndroidRuntime(4624):     ... 11 more

1 个答案:

答案 0 :(得分:5)

您需要在尝试访问该布局中的UI元素之前致电setContentView(R.layout.spinnerex)