Android SDK not compiling apps correctly

2019-08-29 05:46发布

问题:

It seems that the Android SDK doesn't compile my android app correctly. After building an app, I sometimes get seemingly random exceptions. The error goes away if I comment out the offending code, rebuild, uncomment the code, then rebuild again.

For instance, this code gave me a NullPointerException:

playButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { startActivityForResult(settings, PLAY_GAME); } });

There is nothing wrong this code! Yet adb logcat tells me it's the cause of an exception. Commenting out this code, rebuilding, uncommenting and rebuilding, makes the error go away.

What is going on?

This is making developing for android particularly frustrating.

Update: As I pointed out before, these are seemingly random errors that go away once I uncomment/recompile/comment/recompile. So I can't reproduce the stacktrace. It was a NullPointerException if I remember correctly.

However, if it is indeed buggy programming, why is the app running fine now w/o ANY errors at all?

Whenever I make a significant change to the app (add a new resource, a new class, function, etc), old code that was working fine breaks. Why?

Update: Seriously guys, lay off on the hate. I feel like a stumbled on a Atheists vs Christian debate. Sorry to have insunated that android was at fault. Started a new question with the latest errors:

App Ran Fine, Now Breaking for Seemingly Unknown Reasons

回答1:

Commenting out this code, rebuilding, uncommenting and rebuilding, makes the error go away.

This might seem to you like a bug in Android, but I suspect that the real problem is a threading / synchronization related bug in your code. For instance, playButton may be being used before it is initialized.

But we cannot really help you unless you show us the stack trace and identify the line(s) of code where the exception was thrown.


However, if it is indeed buggy programming, why is the app running fine now w/o ANY errors at all?

See above: it is most likely because you have a threading / synchronization issue. You need to be aware that different parts of an application with a GUI will run in different threads ... even if you didn't explicitly create those threads. There are rules you have to follow. If you don't follow those rules, bad things happen ... like exceptions that sometimes happen and sometimes don't happen, depending on which way the wind is blowing.



回答2:

Could be that your PLAY_GAME activity is returning without a result, e.g., if you press the back button & it just stops without calling setResult().



回答3:

No, as noted in several other places, sometimes the source and the bin get out of sync. I've noticed numerous times, specially when making changes to XML. The solution, for commandline users, is to remove the bin directory, and re-compile. Problem goes away.