[mono-android] NoSuchMethodError

Jonathan Pryor jonp at xamarin.com
Fri Jan 6 09:12:43 EST 2012


On Jan 5, 2012, at 7:04 PM, Alex Lerner wrote:
> I am calling Stop() or Destroy() methods on Java.Lang.Thread object.  

Firstly, why are you using Java.Lang.Thread instead of System.Threading.Thread?

> I get back:     Any ideas?  i dont think its me....
> 
> Java.Lang.NoSuchMethodError: Exception of type 'Java.Lang.NoSuchMethodError' was thrown.
> D/SFEngine(11491):   at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod) [0x00000] in <filename unknown>:0 
> D/SFEngine(11491):   at Java.Lang.Thread.Destroy () [0x00000] in <filename unknown>:0 
> D/SFEngine(11491):   at StarFighter.SFEngine.onExit (Android.Views.View v) [0x00000] in <filename unknown>:0 
> D/SFEngine(11491):   --- End of managed exception stack trace ---
> D/SFEngine(11491): java.lang.NoSuchMethodError: Thread.destroy()
> D/SFEngine(11491): 	at java.lang.Thread.destroy(Thread.java:486)
> D/SFEngine(11491): 	at mono.android.view.View.OnClickListenerImplementor.n_onClick(Native Method)

So, we actually got to java.lang.Thread.destroy(), as it's in the stack trace.

This is hilarious, you're going to love this: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/Thread.java#Thread.destroy%28%29

    /**
     * Destroys the receiver without any monitor cleanup.
     *
     * @deprecated Not implemented.
     */
    @Deprecated
    public void destroy() {
        throw new NoSuchMethodError("Thread.destroy()"); // TODO Externalize???
    }

The link is to the OpenJDK source (as I can't find an online link to the Android libcore source), while the latter code is from libcore/luni/src/main/java/java/lang/Thread.java (should you happen to have a `repo` checkout).

You're getting a NoSuchMethodError() because Thread.destroy() throws a NoSuchMethodError. :-)

In short, don't call it. Thread.Stop() should work -- at least, it _is_ actually implemented in Android -- but I would still recommend using System.Threading.Thread over Java.Lang.Thread.

 - Jon



More information about the Monodroid mailing list