Getting NullPointerException with AppCompatActivity after june 2nd update.

AntonKAntonK USMember

After June 2nd Xamarin platform update I'm getting NullPointerException, when AppCompatActivity starts.
Workaround is to use older version of Xamarin.Android. But I wanna know what's wrong.

[MonoDroid] UNHANDLED EXCEPTION: [MonoDroid] Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown. [MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x0004b> [MonoDroid] at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue*) [0x00084] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1029 [MonoDroid] at Android.App.Activity.SetContentView (int) [0x00070] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:5466 [MonoDroid] at Divvy.Android.UI.Activities.SplashActivity.OnCreate (Android.OS.Bundle) [0x00009] in d:\Projects\divvyMobile\Divvy.Android\UI\Activities\SplashActivity.cs:22 [MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:2707 [MonoDroid] at (wrapper dynamic-method) object.e2dd4570-1bf5-41c3-8251-c245e04b892f (intptr,intptr,intptr) <IL 0x00017, 0x00027> [MonoDroid] --- End of managed exception stack trace --- [MonoDroid] java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.internal.widget.ContentFrameLayout.setId(int)' on a null object reference [MonoDroid] at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:389) [MonoDroid] at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:246) [MonoDroid] at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106) [MonoDroid] at md5feaba49227f182b4a9f46be73a17f454.SplashActivity.n_onCreate(Native Method) [MonoDroid] at md5feaba49227f182b4a9f46be73a17f454.SplashActivity.onCreate(SplashActivity.java:31) [MonoDroid] at android.app.Activity.performCreate(Activity.java:5990) [MonoDroid] at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) [MonoDroid] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) [MonoDroid] at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) [MonoDroid] at android.app.ActivityThread.access$800(ActivityThread.java:151) [MonoDroid] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) [MonoDroid] at android.os.Handler.dispatchMessage(Handler.java:102) [MonoDroid] at android.os.Looper.loop(Looper.java:135) [MonoDroid] at android.app.ActivityThread.main(ActivityThread.java:5254) [MonoDroid] at java.lang.reflect.Method.invoke(Native Method) [MonoDroid] at java.lang.reflect.Method.invoke(Method.java:372) [MonoDroid] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) [MonoDroid] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Activity code:

[Activity(MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait, NoHistory = true, Theme="@style/AppTheme.NoActionBar")]      
    public class SplashActivity : AppCompatActivity, IOnMapReadyCallback
    {
        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
            SetContentView (Resource.Layout.activity_splash);

            InitDatabase ();

            if (!string.IsNullOrEmpty (AccountUtils.GetCurrentUserSessionId (this)))
                StartActivity (typeof(HomeActivity));
            else
                StartActivity (typeof(LoginActivity));
        }
}

Posts

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    Something related to layout I'd say. What is it?

  • AntonKAntonK USMember

    layout
    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/material_grey_100"> <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/logo" android:layout_centerInParent="true" /> <RelativeLayout android:layout_width="0dp" android:layout_height="0dp" android:id="@+id/map_stub" /> </RelativeLayout>

    Works ok if I extend Activity instead AppCompatActivity.
    Also works on Xamarin.Android 3.11.458 + Xamarin Studio 5.9.0

  • SeanMcElroySeanMcElroy USMember

    I can firm exactly the same issue with Xamarin Studio 5.9.5 and Xamarin.Android 5.1.5

    Stacktrace:

                            System.Diagnostics.Debugger.Mono_UnhandledException (ex={Java.Lang.RuntimeException: Exception of type 'Java.Lang.RuntimeException' was thrown.
    
                              --- End of managed exception stack trace ---
                            java.lang.RuntimeException: Unable to start activity ComponentInfo{XXX.XXXXXXXXX/md59daf6ef956000df99d5293e170f86256.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.internal.widget.ContentFrameLayout.setId(int)' on a null object reference
                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
                                at android.app.ActivityThread.access$1000(ActivityThread.java:198)
                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                at android.os.Looper.loop(Looper.java:145)
                                at android.app.ActivityThread.main(ActivityThread.java:6837)
                                at java.lang.reflect.Method.invoke(Native Method)
                                at java.lang.reflect.Method.invoke(Method.java:372)
                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.internal.widget.ContentFrameLayout.setId(int)' on a null object reference
                                at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:392)
                                at android.support.v7.app.AppCompatDelegateImplV7.onPostCreate(AppCompatDelegateImplV7.java:166)
                                at android.support.v7.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:65)
                                at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1201)
                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101)
                                ... 10 more
                            }) in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Diagnostics/Debugger.cs:122
    

    Layout:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <TableLayout ...>
            ...
            </TableLayout>
        </android.support.v4.widget.NestedScrollView>
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBar"
            android:layout_height="192dp"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:background="?attr/colorPrimary">
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginStart="16dp"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginBottom="32dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
                <ImageView
                    android:id="@+id/profileImage"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:clickable="true"
            android:src="@drawable/ic_action_new"
            app:layout_anchor="@id/appBar"
            app:layout_anchorGravity="bottom|right|end"
            app:elevation="12dp"
            app:borderWidth="0dp" />
    </android.support.design.widget.CoordinatorLayout>
    
  • asainathasainath USMember
    edited December 2015

    @AntonK were you able to find a solution for this issue? If I may add, this happened to me after I upgraded my SDK build tools / Support Library / Support Repository and not after a Xamarin update.

  • asainathasainath USMember

    I was able to find a solution to my problem. As I had stated, the problem started showing up after an update to the SupportLibrary and BuildTools. In my case, I had a dependency on an external package called PagerSlidingTabStrip that wasn't compatible with API 23. It took me sometime to realize that but once I updated the support library API's and removed this project dependency - I did not get this error again.

  • keerthi17keerthi17 INMember

    I would like to pick the file using file picker concept on xamarin.forms(PCL) and passing the selected pdf file path from PCL to android using dependency. After getting the path on native, I would like to show the selected pdf file on the created webview using layout on native android. I did this concept, but after starting the activity, it was showing "attempt to invoke virtual method on a null object reference" exception. I attached the code for your reference,

    using System;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using FilePickerSampleAndroid.Droid;
    using Xamarin.Forms;
    
    [assembly: Dependency(typeof(MainPdfClass))]
    namespace FilePickerSampleAndroid.Droid
    {
        public class MainPdfClass : Activity , IPdfCreator
        {
            public Android.Webkit.WebView webView;
            public string path;
            public void ShowPdfFile(string fileLocation)
            {
                path = fileLocation;
                //this.OnCreate(new Bundle());
                Intent intent = new Intent(this, typeof(MainPdfClass));
                StartActivity(intent);
                //OnCreate(new Android.OS.Bundle());
            }
    
            protected override void OnCreate(Android.OS.Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.Main);
                webView.Settings.JavaScriptEnabled = true;
                webView.LoadUrl(path);
            }
        }
    }
    

    Kindly check it, if did mistakes??

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    I can confirm that this problem does not exist in version 25.4.0.2 of Xamarin.Android.Support.Design. It may work in more recent versions. I'm not sure.

    It's a bug in Xamarin.

    It's easy to replicate. Just create a new sample project in visual studio and the problem comes up.

Sign In or Register to comment.