views:

106

answers:

3
package one.two;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;


public class Booking extends Activity
{
    private DBAdapter db; 
    private Spinner colourSpinner;
    public Cursor c;
    public TextView txtArrival;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        db = new DBAdapter(this);
        this.setContentView(R.layout.booking);
        db.open();
        fillData();
        db.close();

        Spinner colourSpinner = (Spinner) findViewById(R.id.myspinner);
    }

    private void fillData()
    {
        Cursor c = db.getSpinnerData();
        startManagingCursor(c);
        String[] from = new String[]{DBAdapter.KEY_ARRIVAL};
        int[] to = new int[]{android.R.id.text1};
        SimpleCursorAdapter adapter =
        new SimpleCursorAdapter(this,R.layout.booking, c, from, to );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        Spinner colourSpinner = (Spinner) findViewById(R.id.myspinner);
        colourSpinner.setAdapter(adapter);
    }
    public class MyOnItemSelectedListener implements OnItemSelectedListener
    {
        public void onItemSelected(AdapterView<?> arg0, View v,int position, long id)
        {
            if (Booking.this.c.moveToPosition(position)) {
                String title=Booking.this.c.getString(1);
                Booking.this.txtArrival.setText(title);
            }

        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
            // TODO Auto-generated method stub

        }
    };
}

Error log

 08-06 01:27:34.874: ERROR/AndroidRuntime(721): Uncaught handler: thread main exiting due to uncaught exception
08-06 01:27:34.893: ERROR/AndroidRuntime(721): java.lang.NullPointerException
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at one.two.Booking$MyOnItemSelectedListener.onItemSelected(Booking.java:53)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:856)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.widget.AdapterView.access$200(AdapterView.java:41)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:827)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.os.Handler.handleCallback(Handler.java:587)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.os.Looper.loop(Looper.java:123)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at android.app.ActivityThread.main(ActivityThread.java:3948)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at java.lang.reflect.Method.invoke(Method.java:521)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-06 01:27:34.893: ERROR/AndroidRuntime(721):     at dalvik.system.NativeStart.main(Native Method)

Thank you.

A: 

If Booking.this.c.moveToPosition(position) returns false, you won't set any text. Are you certain it returns true?

You can check this by changing:

if (Booking.this.c.moveToPosition(position)) {
    String title=Booking.this.c.getString(1);
    Booking.this.txtArrival.setText(title);
}

to:

boolean result = Booking.this.c.moveToPosition(position);
if (result) {
    String title=Booking.this.c.getString(1);
    Booking.this.txtArrival.setText(title);
}

and use a debugger or a dialog box or something to display the value of result before you test it.

Rich
Sorry but how do i check if it returns true?
User358218
Ok thank you!=)
User358218
+1  A: 

Try calling setOnItemSelectedListener for your spinner with an instance of your custom class.. right now the code inside MyOnItemSelectedListener is not being invoked.

You can modify your onCreate to something like below.

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    db = new DBAdapter(this);
    this.setContentView(R.layout.booking);
    db.open();
    fillData();
    db.close();

    Spinner colourSpinner = (Spinner) findViewById(R.id.myspinner);
    //Set the OnItemSelectedListener for the Spinner
    colourSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
Quintin Robinson
Ok let me try that, and i'll get back to you.
User358218
An error log pops out. Updated the codes
User358218
+1  A: 

In the method fillData() you are creating a local Cursor object "Cursor c", which hides your class member "public Cursor c". You are initializing the local variable and when onItemSelected is called Booking.this.c will be null which gives you the NullPointerException.

Removing the "Cursor" part in fillData() should avoid this.

private void fillData()
{
   c = db.getSpinnerData();
   ...
}
Key
Ok let me try it. Thank you
User358218
After i removed Cursor, the error goes to line 53, which is Booking.this.txtArrival.setText(title);
User358218
txtArraival has not been initialized either, so it is also null. If the TextView you want to use is within R.layout.booking you can add something like "txtArraival = (TextView) findViewById(R.id.'view_id');" in onCreate where 'view_id' should be replaced with the id you have.
Key
Yea i added it to the oncreate method. Still a null error appears. TextView txtArrival = (TextView) findViewById(R.id.txtArrival);
User358218
Got the stack trace?
Key
Yea. Edited the codes.
User358218
Could you update the code as well? Also, it's hard to tell which line 53 is.
Key
Lets continue this in your new question. :)
Key