mail-archives/monodroid/2011-December/007597.html

111 строки
5.0 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE> [mono-android] Constructor became protected in 4.0 : Android.App.AlertDialog.AlertDialog(System.IntPtr, Android.Runtime.JniHandleOwnership)
</TITLE>
<LINK REL="Index" HREF="index.html" >
<LINK REL="made" HREF="mailto:monodroid%40lists.ximian.com?Subject=%5Bmono-android%5D%20Constructor%20became%20protected%20in%204.0%20%3A%0A%09Android.App.AlertDialog.AlertDialog%28System.IntPtr%2C%0A%09Android.Runtime.JniHandleOwnership%29&In-Reply-To=1323190295626-5052707.post%40n5.nabble.com">
<META NAME="robots" CONTENT="index,nofollow">
<META http-equiv="Content-Type" content="text/html; charset=us-ascii">
<LINK REL="Previous" HREF="007595.html">
<LINK REL="Next" HREF="007600.html">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>[mono-android] Constructor became protected in 4.0 : Android.App.AlertDialog.AlertDialog(System.IntPtr, Android.Runtime.JniHandleOwnership)</H1>
<B>Jonathan Pryor</B>
<A HREF="mailto:monodroid%40lists.ximian.com?Subject=%5Bmono-android%5D%20Constructor%20became%20protected%20in%204.0%20%3A%0A%09Android.App.AlertDialog.AlertDialog%28System.IntPtr%2C%0A%09Android.Runtime.JniHandleOwnership%29&In-Reply-To=1323190295626-5052707.post%40n5.nabble.com"
TITLE="[mono-android] Constructor became protected in 4.0 : Android.App.AlertDialog.AlertDialog(System.IntPtr, Android.Runtime.JniHandleOwnership)">jonp at xamarin.com
</A><BR>
<I>Tue Dec 6 12:10:24 EST 2011</I>
<P><UL>
<LI>Previous message: <A HREF="007595.html">[mono-android] Constructor became protected in 4.0 : Android.App.AlertDialog.AlertDialog(System.IntPtr, Android.Runtime.JniHandleOwnership)
</A></li>
<LI>Next message: <A HREF="007600.html">[mono-android] Constructor became protected in 4.0 : Android.App.AlertDialog.AlertDialog(System.IntPtr, Android.Runtime.JniHandleOwnership)
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#7597">[ date ]</a>
<a href="thread.html#7597">[ thread ]</a>
<a href="subject.html#7597">[ subject ]</a>
<a href="author.html#7597">[ author ]</a>
</LI>
</UL>
<HR>
<!--beginarticle-->
<PRE>On Dec 6, 2011, at 11:51 AM, klimaye wrote:
&gt;<i> I was using the constructor below to integrate with zxing bar code scanner.
</I>&gt;<i>
</I>&gt;<i> IntPtr rAlertDialog = JNIEnv.CallStaticObjectMethod(
</I>&gt;<i> IntentResult, IntentResult_initiateScan, new
</I>&gt;<i> JValue(activity));
</I>&gt;<i> // ...and construct a nice managed wrapper over the Java
</I>&gt;<i> instance.
</I>&gt;<i> new AlertDialog(rAlertDialog, JniHandleOwnership.DoNotTransfer);
</I>
Given that JNIEnv.CallStaticObject() returns a local ref, unless you're calling JNIEnv.DeleteLocalRef() later in the code, you really want JniHandleOwnership.TransferLocalRef.
The new way is the same as the old abstract-class-or-interface way:
var dialog = new Java.Lang.Object (rAlertDialog, JniHandleOwnership.TransferLocalRef)
.JavaCast&lt;AlertDialog&gt;();
&gt;<i> Why was this done? What is an alternate way of achieving above now in 4.0?
</I>
The primary reason is to reduce user error. &quot;Back in the day,&quot; all non-abstract types had a public IntPtr constructor, and often this was the _only_ constructor on the type. This resulted in numerous people doing the (very sane):
var value = new SomeType (IntPtr.Zero);
value.SomeMethod();
and wondering why `value.SomeMethod()` dies with a NullPointerException. In short, while this worked, it was very brittle.
Later, we kept the constructors, but renamed the parameter to &quot;doNotUse&quot; so people wouldn't use it. That wasn't really good either. :-)
Since we had to change all the constructors anyway (to add the JniHandleOwnership parameter, which works around a JNI limitation), we figured we'd make the constructors protected as well, thus removing the ability to easily &quot;misuse&quot; the constructor. This also increases consistency with abstract types and interfaces, neither of which could be instantiated via `new` anyway and required the JavaCast extension method.
- Jon
</PRE>
<!--endarticle-->
<HR>
<P><UL>
<!--threads-->
<LI>Previous message: <A HREF="007595.html">[mono-android] Constructor became protected in 4.0 : Android.App.AlertDialog.AlertDialog(System.IntPtr, Android.Runtime.JniHandleOwnership)
</A></li>
<LI>Next message: <A HREF="007600.html">[mono-android] Constructor became protected in 4.0 : Android.App.AlertDialog.AlertDialog(System.IntPtr, Android.Runtime.JniHandleOwnership)
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#7597">[ date ]</a>
<a href="thread.html#7597">[ thread ]</a>
<a href="subject.html#7597">[ subject ]</a>
<a href="author.html#7597">[ author ]</a>
</LI>
</UL>
<hr>
<a href="http://lists.ximian.com/mailman/listinfo/monodroid">More information about the Monodroid
mailing list</a><br>
</body></html>