Reputation: 189
I would like to put a TextView
on the menu of my Android app which displays a simple text with days remaining until a certain date. This is what I have tried so far:
protected override void OnCreate (Bundle bundle){
base.OnCreate (bundle);
TextView nochxtage = FindViewById<TextView>(Resource.Id.nochxtage);
DateTime daysLeft = DateTime.Parse("4/10/2016 0:00:00 AM");
DateTime startDate = DateTime.Now;
TimeSpan t = daysLeft - startDate;
string countdown = string.Format("{0} days remain.", t.Days);
nochxtage.Text = countdown;
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main1);`
}
The problem is that the app immediately crashes once the Activity tries to start up. Anyone has got an idea? Thank you very much in advance!
Edit: This is the Debug Log:
Loaded assembly: /storage/emulated/0/Android/data/com.mexeroser.testapp/files/.__override__/Testapp.dll
Loaded assembly: /storage/emulated/0/Android/data/com.mexeroser.testapp/files/.__override__/Mono.Android.dll [External]
02-20 12:23:56.680 I/art ( 1805): Late-enabling -Xcheck:jni
02-20 12:23:57.070 W/monodroid( 1805): Using override path: /data/data/com.mexeroser.testapp/files/.__override__
02-20 12:23:57.070 W/monodroid( 1805): Using override path: /storage/emulated/0/Android/data/com.mexeroser.testapp/files/.__override__
02-20 12:23:57.070 W/monodroid( 1805): Trying to load sgen from: /data/data/com.mexeroser.testapp/files/.__override__/libmonosgen-2.0.so
02-20 12:23:57.070 W/monodroid( 1805): Trying to load sgen from: /storage/emulated/0/Android/data/com.mexeroser.testapp/files/.__override__/libmonosgen-2.0.so
02-20 12:23:57.070 W/monodroid( 1805): Trying to load sgen from: /data/app/com.mexeroser.testapp-1/lib/arm/libmonosgen-2.0.so
02-20 12:23:57.070 W/monodroid( 1805): Trying to load sgen from: /data/data/com.mexeroser.testapp/files/.__override__/links/libmonosgen-2.0.so
02-20 12:23:57.080 W/monodroid-debug( 1805): Trying to initialize the debugger with options: --debugger-agent=transport=dt_socket,loglevel=0,address=127.0.0.1:8981,server=y,embedding=1
02-20 12:23:57.100 W/monodroid-debug( 1805): Accepted stdout connection: 31
02-20 12:23:57.990 W/monodroid-gc( 1805): GREF GC Threshold: 46080
Loaded assembly: /storage/emulated/0/Android/data/com.mexeroser.testapp/files/.__override__/System.Core.dll [External]
Loaded assembly: MonoDroidConstructors [External]
02-20 12:24:05.930 I/art ( 1805): Can not find class: Landroid/widget/ViewStub;
02-20 12:24:05.930 I/art ( 1805): Can not find class: Landroid/webkit/ViewStub;
02-20 12:24:05.930 I/art ( 1805): Can not find class: Landroid/app/ViewStub;
02-20 12:24:05.940 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.150 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.150 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.160 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.160 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.170 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.170 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.180 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.180 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.180 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.180 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.190 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.190 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.190 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.190 E/ZipFileCache( 1805): init failed when open zip file.
02-20 12:24:06.470 I/MonoDroid( 1805): UNHANDLED EXCEPTION:
02-20 12:24:06.480 I/MonoDroid( 1805): System.NullReferenceException: Object reference not set to an instance of an object
02-20 12:24:06.480 I/MonoDroid( 1805): at Testapp.MainActivity.OnCreate (Android.OS.Bundle) [0x00046] in C:\Users\Markus\Documents\Testapp\Testapp\MainActivity.cs:28
02-20 12:24:06.480 I/MonoDroid( 1805): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.Activity.cs:2212
02-20 12:24:06.480 I/MonoDroid( 1805): at (wrapper dynamic-method) object.7ffb6a7b-4f79-4d31-9a83-18e5c945f2d4 (intptr,intptr,intptr) <IL 0x00017, 0x0004b>
02-20 12:24:06.490 W/art ( 1805): JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
An unhandled exception occured.
02-20 12:24:11.000 E/AndroidRuntime( 1805): FATAL EXCEPTION: main
02-20 12:24:11.000 E/AndroidRuntime( 1805): Process: com.mexeroser.testapp, PID: 1805
02-20 12:24:11.000 E/AndroidRuntime( 1805): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
02-20 12:24:11.000 E/AndroidRuntime( 1805): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
02-20 12:24:11.000 E/AndroidRuntime( 1805): Caused by: java.lang.reflect.InvocationTargetException
02-20 12:24:11.000 E/AndroidRuntime( 1805): at java.lang.reflect.Method.invoke(Native Method)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at java.lang.reflect.Method.invoke(Method.java:372)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
02-20 12:24:11.000 E/AndroidRuntime( 1805): ... 1 more
02-20 12:24:11.000 E/AndroidRuntime( 1805): Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
02-20 12:24:11.000 E/AndroidRuntime( 1805): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.Activity.cs:2212
02-20 12:24:11.000 E/AndroidRuntime( 1805): at (wrapper dynamic-method) object.7ffb6a7b-4f79-4d31-9a83-18e5c945f2d4 (intptr,intptr,intptr) <IL 0x00017, 0x0004b>
02-20 12:24:11.000 E/AndroidRuntime( 1805):
02-20 12:24:11.000 E/AndroidRuntime( 1805): at md53cfe2f6a053d0eea6a87b979803e0d92.MainActivity.n_onCreate(Native Method)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at md53cfe2f6a053d0eea6a87b979803e0d92.MainActivity.onCreate(MainActivity.java:28)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at android.app.Activity.performCreate(Activity.java:6013)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at android.app.ActivityThread.access$1200(ActivityThread.java:152)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at android.os.Handler.dispatchMessage(Handler.java:102)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at android.os.Looper.loop(Looper.java:135)
02-20 12:24:11.000 E/AndroidRuntime( 1805): at android.app.ActivityThread.main(ActivityThread.java:5538)
02-20 12:24:11.000 E/AndroidRuntime( 1805): ... 4 more
Upvotes: 0
Views: 106
Reputation: 4746
Try putting your SetContentView
at the top, instead of at the bottom, like below:-
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main1);`
TextView nochxtage = FindViewById<TextView>(Resource.Id.nochxtage);
DateTime daysLeft = DateTime.Parse("4/10/2016 0:00:00 AM");
DateTime startDate = DateTime.Now;
TimeSpan t = daysLeft - startDate;
string countdown = string.Format("Noch {0} Tage bis zur ersten Aufführung.", t.Days);
nochxtage.Text = countdown;
Upvotes: 0