0

My second book for Android programming Hello, Android by Ed Burnette. I'm using eclipse. The code matches the book and it matches the code downloaded from the website of the book. But I know I'm doing something wrong here. I added a bunch of breakpoints where I figure (mostly guessing) where the problem might be happening. What I've come to is that this line of code is the culprit (SudokuActivity.java line 21) You can download the entire code here http://kbsoftware.dlinkddns.com/Sudoku.zip

aboutButton.setOnClickListener(this);

but I just can't figure out why ? It must be the result of something I'm doing wrong somewhere else. I've deleted and recreated the avd and that made no difference so not it. I'm at a lost here.

public class SudokuActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    View aboutButton = findViewById(R.id.about_button);
    aboutButton.setOnClickListener(this);
}    

I want to thank everyone who responded, it's all fixed and working and I could not have done it without your help. I've learned more working on this problem then I would of in weeks if not months of problem free programming.

2 Answers2

1

Yout aboutButton is not getting bound properly.

Do something like

Button aboutButton = (Button) findViewById(R.id.about_button);

mastDrinkNimbuPani
  • 1,249
  • 11
  • 14
0

I downloaded your code and it runs correctly on my phone. So if your code is the same it should run. It seems that findViewById didn't find the view and then calling a method on a null object caused the nullpointerexception.

My dumb question: have you tried a project clean up? You can even try saving your classes, deleting the project and creating a new one. Hope it helps

nax83
  • 274
  • 3
  • 15
  • After reading your reply I was wondering how important are the order of things between the < /> in the main.xml file. So I copied and pasted to move the android:id which was at the bottom and move it to the top and then moved android:text to the bottom. And voila it now all works. Seem this order is a big deal to the avd. – user1355452 Apr 26 '12 at 01:36
  • @user1355452 No wait a moment, it needs more explanation. The attributes order in XML is not significant. More here [link](http://stackoverflow.com/a/726933/1357273). I bet that there was a clean up problem as I told you. The Android compiler builds automatically the resources files (i.e. the XML) in a class called R. This class, normally, is compiled again any time you change your resources. So when you changed the xml moving the id, it compiled again the resources and this solved the problem. You can try moving back the id where it was and try to run your example again. It should work! – nax83 Apr 26 '12 at 20:28
  • I did use the project->clean options, uninstalled everything even deleted eclipse and after reinstalling it still did not work. I know about the warnings for R id and R.java file but never considered it for this problem. I did move the id's around and it still worked nice. The R id problem makes me want to switch to Windows Phone but I'm too stubborn for that :) – user1355452 Apr 30 '12 at 23:20