I have a very simple android application started and any time i add
any sort of a listener to anything, the android emulator tells me my
application has stopped, before it even does anything...
public class StatTracker extends Activity {
ArrayAdapter<Record> aa;
ArrayList<Record> records;
ListView recordListView;
Record selectedRecord;
EditText myEdit;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
records = new ArrayList<Record>();
recordListView = (ListView)this.findViewById
(R.id.recordListView);
recordListView.setOnItemClickListener(new OnItemClickListener
() {
public void onItemClick(AdapterView<?> av, View v, int
index,
long arg3) {
selectedRecord = records.get(index);
Toast.makeText(StatTracker.this,
selectedRecord.toString(),
Toast.LENGTH_SHORT);
}
});
int layoutID = android.R.layout.simple_list_item_1;
aa = new ArrayAdapter<Record>(this, layoutID, records);
recordListView.setAdapter(aa);
myEdit = (EditText)findViewById(R.id.opponent_edit);
//removing the listener below will at least allow the
application to run
myEdit.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent
event) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
if(keyCode ==
KeyEvent.KEYCODE_DPAD_CENTER) {
Record _r = new Record( new
Opponent( myEdit.getText().toString
()));
myEdit.setText("");
addRecord(_r);
return true;
}
return false;
}
});
}
private void addRecord(Record r) {
records.add(r);
aa.notifyDataSetChanged();
}
private static final int MENU_ADD = Menu.FIRST;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_ADD, Menu.NONE, R.string.menu_add);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch( item.getItemId() ) {
case MENU_ADD: {
setContentView(R.layout.new_record_view);
return true;
}
}
return false;
}
}
i am trying to follow most conventions used in the book Professional
Android Application Development, but a critique of my code can never
hurt.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---