Bug 33231 - Setting text using TextView.Text on a TextView not tied to a layout after calling Measure() causes null pointer exception.
Summary: Setting text using TextView.Text on a TextView not tied to a layout after cal...
Status: RESOLVED UPSTREAM
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 5.1
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-08-19 11:59 UTC by David Schulte
Modified: 2015-08-19 14:19 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Sample solution demonstrating this bug. (37.03 KB, application/zip)
2015-08-19 12:00 UTC, David Schulte
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED UPSTREAM

Description David Schulte 2015-08-19 11:59:40 UTC
The following code fragment causes a null pointer exception, which seems to be an issue with Xamarin Android. This bug
seems to be due to the fact that the TextView is not linked into a parent layout. If you add the TextView to a parent
layout before invoking this fragment the null pointer exception does not occur.

I've attached a sample solution that you can build to reproduce this.

	TextView textView = new TextView(this);
	textView.Text = "abc";
	textView.Measure(0, 0);
	int width = textView.MeasuredWidth;
	textView.Text = "xyz";	// Generates a null pointer exception.

Exception:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
[MonoDroid] at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr,Android.Runtime.JValue*) [0x00063] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:571
[MonoDroid] at Android.Widget.TextView.set_TextFormatted (Java.Lang.ICharSequence) [0x0004b] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.TextView.cs:2700
[MonoDroid] at Android.Widget.TextView.set_Text (string) [0x00013] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.TextView.cs:2711
[MonoDroid] at TextViewMeasureTest.MainActivity.OnCreate (Android.OS.Bundle) [0x00047] in /Users/xxx/Workspaces/Xamarin/TextViewMeasureTest/TextViewMeasureTest/MainActivity.cs:29
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:2475
[MonoDroid] at (wrapper dynamic-method) object.c4a690ab-f059-4630-877a-b43c52861fd2 (intptr,intptr,intptr) <IL 0x00017, 0x00043>
[MonoDroid]   --- End of managed exception stack trace ---
[MonoDroid] java.lang.NullPointerException
[MonoDroid] 	at android.widget.TextView.checkForRelayout(TextView.java:7965)
[MonoDroid] 	at android.widget.TextView.setText(TextView.java:4841)
[MonoDroid] 	at android.widget.TextView.setText(TextView.java:4673)
[MonoDroid] 	at android.widget.TextView.setText(TextView.java:4648)
[MonoDroid] 	at md5deb8717d211bf7919897f6213f346a36.MainActivity.n_onCreate(Native Method)
[MonoDroid] 	at md5deb8717d211bf7919897f6213f346a36.MainActivity.onCreate(MainActivity.java:28)
[MonoDroid] 	at android.app.Activity.performCreate(Activity.java:5426)
[MonoDroid] 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
[MonoDroid] 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
[MonoDroid] 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
[MonoDroid] 	at android.app.ActivityThread.access$900(ActivityThread.java:161)
[MonoDroid] 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
[MonoDroid] 	at android.os.Handler.dispatchMessage(Handler.java:102)
[MonoDroid] 	at android.os.Looper.loop(Looper.java:157)
[MonoDroid] 	at android.app.ActivityThread.main(ActivityThread.java:5356)
[MonoDroid] 	at java.lang.reflect.Method.invokeNative(Native Method)
[MonoDroid] 	at java.lang.reflect.Method.invoke(Method.java:515)
[MonoDroid] 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
[MonoDroid] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
[MonoDroid] 	at dalvik.system.NativeStart.main(Native Method)
[AndroidRuntime] Shutting down VM

Xamarin details:
=== Xamarin Studio ===

Version 5.9.5 (build 9)
Installation UUID: 6f7c36db-b063-41d3-aa0d-4184811018e8
Runtime:
	Mono 4.0.3 ((detached/d6946b4)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400030020

=== Apple Developer Tools ===

Not Found

=== Xamarin.iOS ===

Version: 8.10.4.46 (Starter Edition)
Hash: 2c66d2f
Branch: master
Build date: 2015-08-04 13:52:25-0400

=== Xamarin.Android ===

Version: 5.1.5.3 (Business Edition)
Android SDK: /Users/xxx/My Android/adt-bundle-mac-x86_64/sdk
	Supported Android versions:
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
Java SDK: /usr
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

=== Xamarin Android Player ===

Not Installed

=== Xamarin.Mac ===

Not Installed

=== Build Information ===

Release ID: 509050009
Git revision: b99cb19a1d3b15d8980c4616f13f837457e5b292
Build date: 2015-07-28 02:01:44-04
Xamarin addins: d53d6eceb828422f3d4aff8547f2f6bab6c9ea58

=== Operating System ===

Mac OS X 10.10.4
Darwin XXX.local 14.4.0 Darwin Kernel Version 14.4.0
    Thu May 28 11:35:04 PDT 2015
    root:xnu-2782.30.5~1/RELEASE_X86_64 x86_64
Comment 1 David Schulte 2015-08-19 12:00:17 UTC
Created attachment 12573 [details]
Sample solution demonstrating this bug.
Comment 2 Jonathan Pryor 2015-08-19 14:19:45 UTC
This is an exception in Java code:

> [MonoDroid]   --- End of managed exception stack trace ---
> [MonoDroid] java.lang.NullPointerException
> [MonoDroid]     at android.widget.TextView.checkForRelayout(TextView.java:7965)
> [MonoDroid]     at android.widget.TextView.setText(TextView.java:4841)
> [MonoDroid]     at android.widget.TextView.setText(TextView.java:4673)
> [MonoDroid]     at android.widget.TextView.setText(TextView.java:4648)

It's a NullPointerException within TextView.checkForRelayout(), which is *probably* this bug:

https://code.google.com/p/android/issues/detail?id=75877