views:

79

answers:

1

I have CameraList class that provides Cameras list like "SONY","VIVOTEK","VIVOTEK 7XXX","PIXORD","TOSHIBA","JVC E-CON"...

And I want to list them in my spinner but i couldnt. there is my codes..

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);
    }
}

My camlist code is

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

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

this is my 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  A: 

You need to call setContentView(R.layout.spinnerex) before attempting to access UI elements in that layout.

Christopher