Maxsteel
Maxsteel

Reputation: 2040

Button action-Android app

I am practicing from the book Hello,Android ed3.There is an example code on creating an action button to display 'About' the game.I have edited all the necessary xml files.I am getting error in the following code.logcat is showing nullpointer exception in line 10: about.Button.setClickListener(this).Please help.Also i have been unable to understand 'this' parameter.Any hep?

public class Sudoku extends Activity implements OnClickListener {
private static final String TAG = "Sudoku";
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      // Set up click listeners for all the button
      View aboutButton = findViewById(R.id.about_button);
      aboutButton.setOnClickListener(this);

   }

   public void onClick(View v) {
          switch (v.getId()) {
          case R.id.about_button:
             Intent i = new Intent(this, About.class);
             startActivity(i);
             break;
    }

} }

Upvotes: 0

Views: 1049

Answers (2)

Naresh J
Naresh J

Reputation: 2137

public class Sudoku extends Activity implements OnClickListener {
private static final String TAG = "Sudoku";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   // Set up click listeners for all the button
   Button aboutButton = (Button) findViewById(R.id.about_button);
  aboutButton.setOnClickListener(this);

}

public void onClick(View v) {
      if(v == aboutButton){
         Intent i = new Intent(this, About.class);
         startActivity(i);
         break;
}

Try this one... just changed 2-3 lines...

Upvotes: 0

San
San

Reputation: 5697

Looks like you dont have "about_button" button in the xml file. Did you give the same name for the button in xml?

Upvotes: 1

Related Questions