Saturday, October 1, 2011

Re: [android-developers] Re: How do I report a bug to htc?

You need to use a Lock such as:
http://developer.android.com/reference/java/util/concurrent/locks/ReentrantLock.html
This sounds like a race conditions, as others have pointed out. These
are usually the hardest to narrow down.

Static variables are not a good idea in general unless you know
exactly what you're doing.

Try adding a lock around your static object, like:

private static Object mMyProtectedVar;
private final Lock mLock = new ReentrantLock();

Then before touching the variable, ALWAYS use the lock like so:

mLock.lock();
mMyProtectedVar.something = something;
mLock.unlock();

Java provides some keywords for making variables and methods thread
safe, although I find a lock to be really clear as to its intended
purpose.


On Sat, Oct 1, 2011 at 6:42 PM, Doug <beafdefx@gmail.com> wrote:
> On Sep 27, 4:23 pm, sebastian_bugiu
> <sebastian.bugiu.reloa...@gmail.com> wrote:
>> I have an application in android market that does not work on htc
>> desire and I am positive the issue is not in my code. Basically I have
>> a private static final field that is initialized to a reference to an
>> object and yet whenever I access that object from another thread it
>> throws a NullPointerException even though it is initialized.
>
> Are you sure you were really using private static final?  Static
> finals are initialized when the class loads and will never change as
> long as that class remains loaded.  All threads would be seeing the
> same value the entire time.
>
> The other code you showed can have a race condition in that first
> thread that assigns status may not have reconciled its local copy of
> the new Status object by the time the second thread tries to access
> it.  You would need to synchronize both accesses or use volatile to
> keep them in sync.
>
> You are almost certainly not running into an HTC error.
>
> Doug
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

--
~ Jeremiah:9:23-24
Android 2D MMORPG: http://solrpg.com/http://www.youtube.com/user/revoltingx

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment