我有Cameralist类,提供相机列表,如“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;
    }
}

这是我的猫
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

最佳答案

在试图访问该布局中的ui元素之前,需要调用setContentView(R.layout.spinnerex)

10-05 22:59