0

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;
    }

} }

4

2 に答える 2

1

xmlファイルに「about_button」ボタンがないようです。xmlのボタンに同じ名前を付けましたか?

于 2012-04-08T07:03:14.750 に答える
0
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;
}

これを試してください... 2〜3行を変更しただけです...

于 2012-04-08T07:19:52.510 に答える