Need help with onClick and intents
<p>I'm working and an app based on the Notepad tutorial...In the tutorial you click on a note in the listview and you are taken to edit were the corresponding information is shown, you then make your changes select confirm..and are taken back to Main.</p>

<p>I added a second activity to the app "contact" now when you click on a note in the listview and you are taken to contact were the corresponding information is shown including a button............ you click the button and are taken to edit.........And this is were my problem pops its ugly head. When i ckick the button a get a blank screen and a forced close popup........</p>

<p>Here is the code from the main taking you to contact....This works the information is shown</p>

<pre><code>protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, Contact.class);
    i.putExtra(NotesDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}
</code></pre>

<p>Here is the code that takes you from contact to edit.....This gives me the error, I'm sure it has to do with "intent.putExtra(NotesDbAdapter.KEY_ROWID, id);" needing to be recoded to work with a onClick compared to a onListitemClick........I'm thinking the id is not being passed over</p>

<pre><code>public void onClick(View v) {
String id = null;
switch (v.getId()) {
case R.id.admin: // doStuff
    Intent intent = new Intent(this, Contact.class);
    intent.putExtra(NotesDbAdapter.KEY_ROWID, id);
    startActivity(intent); 
    break;
</code></pre>

<p>Could someone show me how to do this from a button????</p>

<p>Here is the code from Main, Contacts and Edit..........If you need it .......Thanks</p>

<p>Main.class</p>

<pre><code>public class Main extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;

private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private static final int ABOUT = Menu.FIRST + 2;
private static final int TESTBUTTON = Menu.FIRST + 3;
private static final int CONTACT_ID = Menu.FIRST + 4;
private static final int CANCEL_ID = Menu.FIRST + 5;

private NotesDbAdapter mDbHelper;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());
}

private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{NotesDbAdapter.KEY_NAME, NotesDbAdapter.KEY_RANK};


    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1, R.id.text2};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.mainlistrow, notesCursor, from, to);
    setListAdapter(notes);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, INSERT_ID, 0, R.string.menu_insert);
    menu.add(0, ABOUT, 0, R.string.menu_about);
    menu.add(0, TESTBUTTON, 0, R.string.menu_test);
    return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
        case INSERT_ID:
            createNote();
            return true;
        case ABOUT:        
            menuabout();        
            return true;
        case TESTBUTTON:        
            menutest();        
            return true;    
    }

    return super.onMenuItemSelected(featureId, item);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, CONTACT_ID, 0, R.string.menu_contact);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    menu.add(0, CANCEL_ID, 0, R.string.menu_cancel);

}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteNote(info.id);
            fillData();
            return true;
        case CONTACT_ID:
            contact();
            return true;   
    }
    return super.onContextItemSelected(item);
}

private void menutest() {
    Intent i = new Intent(this, Newmain.class);
    startActivityForResult(i, ACTIVITY_CREATE);
    Log.i(this.toString(), "menu test");
}

private void contact() {
    Intent i = new Intent(this, Contact.class);
    startActivityForResult(i, ACTIVITY_CREATE);
    Log.i(this.toString(), "menu contact");
}    

private void createNote() {
    Intent i = new Intent(this, Admin.class);
    startActivityForResult(i, ACTIVITY_CREATE);
}

public void menuabout() {
    final Dialog dialog = new Dialog(Main.this);
    dialog.setContentView(R.layout.about);
    dialog.setTitle("About NCO Leaders Book");      
    dialog.setCancelable(true);
    Button button = (Button) dialog.findViewById(R.id.AboutButton);
    button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        dialog.dismiss();
    }
              });
              //now that the dialog is set up, it's time to show it    
              dialog.show();
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, Contact.class);
    i.putExtra(NotesDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();
}
</code></pre>

<p>}</p>

<p>Contact.class</p>

<pre><code>    public class Contact extends Activity implements OnClickListener{    
    private EditText mNameText;
    private EditText mRankText;
    private Long mRowId;
    private NotesDbAdapter mDbHelper;
    protected Cursor cursor;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        setContentView(R.layout.contact);

        Button adminButton = (Button) this.findViewById(R.id.admin);
        adminButton.setOnClickListener(this); 

        mNameText = (EditText) findViewById(R.id.name);
        mRankText = (EditText) findViewById(R.id.rank);

        mRowId = (savedInstanceState == null) ? null :
            (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                                    : null;}

          populateFields();
    };


private void populateFields() {
    if (mRowId != null) {
        Cursor note = mDbHelper.fetchsoldiers(mRowId);
        startManagingCursor(note);
        mNameText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_NAME)));
        mRankText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_RANK)));
    }
}

//@Override
public void onClick(View v) {
    String id = null;
    switch (v.getId()) {
    case R.id.admin: // doStuff
        Intent intent = new Intent(this, Edit.class);
        intent.putExtra(NotesDbAdapter.KEY_ROWID, id);
        startActivity(intent);
        Log.i("Onclick", "EndingTestNext");    
        break;
}}

}
</code></pre>

<p>Edit.class</p>

<pre><code>public class Edit extends Activity {

private EditText mNameText;
private EditText mRankText;
private EditText mSsnText;
private Long mRowId;
private NotesDbAdapter mDbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();

    setContentView(R.layout.edit);
    setTitle(R.string.edit_note);

    mNameText = (EditText) findViewById(R.id.name);
    mRankText = (EditText) findViewById(R.id.rank);
    mSsnText = (EditText) findViewById(R.id.ssn);

    Button confirmButton = (Button) findViewById(R.id.confirm);


    mRowId = (savedInstanceState == null) ? null :
        (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
    if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                                : null;
    }

    populateFields();

    confirmButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            setResult(RESULT_OK);
            finish();
        }

    });
}

private void populateFields() {
    if (mRowId != null) {
        Cursor note = mDbHelper.fetchNote(mRowId);
        startManagingCursor(note);
        mNameText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_NAME)));
        mRankText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_RANK)));
        mSsnText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_SSN)));
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    saveState();
    outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}

@Override
protected void onPause() {
    super.onPause();
    saveState();
}

@Override
protected void onResume() {
    super.onResume();
    populateFields();
}

private void saveState() {
    String name = mNameText.getText().toString();
    String rank = mRankText.getText().toString();
    String ssn = mSsnText.getText().toString();

    if (mRowId == null) {
        long id = mDbHelper.createNote(name, rank, ssn);
        if (id &gt; 0) {
            mRowId = id;
        }
    } else {
        mDbHelper.updateNote(mRowId, name, rank, ssn);
    }
}}
</code></pre>
