0

Without the onclick listener implements and its relevant codes, the activity works fine. But when onclick listener is there it just force close.

Here is my code:

public class Names extends ListActivity implements OnClickListener {
    private static String[] FROM = { _ID, NAME, ADDRESS, AGE };
       private static int[] TO = { R.id.rowid, R.id.name, R.id.address, R.id.age};
       private static String ORDER_BY = NAME + " DESC";


       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);

          Cursor cursor = getNames();
          showNames(cursor);

        // Set up click listeners
            View addButton = findViewById(R.id.add_button);
            addButton.setOnClickListener(this);        


       }


       private Cursor getNames() {
          // Perform a managed query.
          return managedQuery(CONTENT_URI, FROM, null, null, ORDER_BY);
       }

       private void showNames(Cursor cursor) {
          // data binding
          SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.names, cursor, FROM, TO);
          setListAdapter(adapter);
       }


      public void onClick(View v) {
         switch (v.getId()) {
            case R.id.add_button:
                Cursor cursor = getNames();
                showNames(cursor );
                break;                      
         }
     }
}

What have i done wrong? many thanks for your help.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Ramon
  • 1
  • 1
  • I think this is related to http://stackoverflow.com/questions/3545007/problem-with-android-button-setonclicklistener – bool.dev Dec 10 '11 at 07:02

1 Answers1

0

In these lines, View addButton = findViewById(R.id.add_button); addButton.setOnClickListener(this);

Specify which view you are using instead of using View. e.g :- Button addButton =(Button) findViewById(R.id.add_button); may be you've not cast on which View's onclicklistener you want the event

Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30