Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not activate JNI Handle exception with Floating action button in Xamarin.Android

I'm trying to integrate FloatingActionButton in my application using the xamarin component. On layout inflation it throws the exception "Could not activate JNI Handle 0xffb8b288 (key_handle 0x3ee746ea) of Java type 'com/refractored/fab/FloatingActionButton' as managed type 'com.refractored.fab.FloatingActionButton'."

enter image description here

layout file

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:fab="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
             android:layout_height="match_parent"> 
    <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" /> 
    <com.refractored.fab.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_margin="16dp"
            android:src="@drawable/ic_edit"
            fab:fab_colorNormal="@color/primary"
            fab:fab_colorPressed="@color/primary_pressed"
            fab:fab_colorRipple="@color/ripple" />
</FrameLayout>

Activity code :

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS; 
using Android.Support.V4.App; 
using ActionBar =  Android.Support.V7.App.ActionBar;
using Android.Text;
using com.refractored.fab;  
using Android.Support.V7.App; 

namespace SampleActionBar
{
    [Activity (Label = "SampleActionBar", MainLauncher = true, Icon = "@mipmap/icon")]
    public class MainActivity : ActionBarActivity, ActionBar.ITabListener
    { 
        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
            InitActionBar (); 
        } 
        private void InitActionBar()
        {
            if (SupportActionBar == null)
                return;

            var actionBar = SupportActionBar;
            actionBar.NavigationMode = (int)ActionBarNavigationMode.Tabs;

            var tab1 = actionBar.NewTab();
            tab1.SetTabListener(this);
            tab1.SetText("ListView");
            actionBar.AddTab(tab1); 
        }
        public void OnTabReselected(ActionBar.Tab tab, Android.Support.V4.App.FragmentTransaction ft)
        {
        } 
        public void OnTabSelected(ActionBar.Tab tab, Android.Support.V4.App.FragmentTransaction ft)
        {
            switch (tab.Text)
            {
            case "ListView":
                ft.Replace(Android.Resource.Id.Content, new ListViewFragment());
                break; 
            }
        } 
        public void OnTabUnselected(ActionBar.Tab tab, Android.Support.V4.App.FragmentTransaction ft)
        { 
        } 
    } 
    public class ListViewFragment:Android.Support.V4.App.Fragment, IScrollDirectorListener, AbsListView.IOnScrollListener
    {
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var root = inflater.Inflate(Resource.Layout.Main, container, false);

            var list = root.FindViewById<ListView>(Android.Resource.Id.List);
            var adapter = new ListViewAdapter(Activity, Resources.GetStringArray(Resource.Array.countries));
            list.Adapter = adapter;

            var fab = root.FindViewById<FloatingActionButton>(Resource.Id.fab);
            //fab.AttachToListView(list, this, this);
            fab.Click += (sender, args) =>
            {
                Toast.MakeText(Activity, "FAB Clicked!", ToastLength.Short).Show();
            };
            return root;
        }
        public void OnScrollDown()
        { 
        }

        public void OnScrollUp()
        { 
        }

        public void OnScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
        { 
        }

        public void OnScrollStateChanged(AbsListView view, ScrollState scrollState)
        { 
        }
    }
}

Target sdk version is 23 and min version is 16.
Upadted android support V4,V7 AppCompat(23.1.1.0) and refractored.fab(1.4.0) package.
Sample code from xamarin component works fine without any issues
What might be the cause for this exception? any suggestion/hints are appreciated.
Thank you

like image 955
Suchith Avatar asked Oct 21 '25 14:10

Suchith


1 Answers

That component has been deprecated in favor of using the Support Library version of the FloatingActionButton. You can remove that component and make sure to include the latest design library package Nuget Link from Nuget (23.1.1.0). Then you should be able to do the following.

<android.support.design.widget.FloatingActionButton
  ...
  android:id="@+id/fab" />

With this in your code using Android.Support.Design.Widget;

like image 139
kevskree Avatar answered Oct 23 '25 05:10

kevskree



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!