Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are creating local variables with the same names as your class members. This leads to the members being hidden by the local ones which is why you are getting NullPointerExceptions. You are simply not giving the members any value which is why they are null.</p> <p>What you want to do is:</p> <pre><code>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); txtArrival = (TextView) findViewById(R.id.txtArrival); // Member assignment colourSpinner = (Spinner) findViewById(R.id.myspinner); // Member assignment db.open(); fillData(); db.close(); colourSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); } private void fillData() { 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 ); // Removed this line, since members is set in onCreate colourSpinner.setAdapter(adapter); } public class MyOnItemSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView&lt;?&gt; arg0, View v,int position, long id) { boolean result = Booking.this.c.moveToPosition(position); if (result) { String title=Booking.this.c.getString(1); Booking.this.txtArrival.setText(title); } } @Override public void onNothingSelected(AdapterView&lt;?&gt; arg0) { // TODO Auto-generated method stub } }; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload