Coder Social home page Coder Social logo

Comments (6)

jpobst avatar jpobst commented on September 27, 2024

This looks like the line that is causing the InvalidCastException:

__args [0] = new JniArgumentValue ((listener == null) ? IntPtr.Zero : ((global::Java.Lang.Object) listener).Handle);

My guess is your implementation of NavigationBarView.IOnItemSelectedListener does not inherit from Java.Lang.Object, which is required.

from androidx.

gmck avatar gmck commented on September 27, 2024

@genis-bosch-ypsomed

The following is a cut-down version that I’ve been using with X.A, Net7 and Net8. See any of the NavigationGraph examples at https://github.com/gmck for more details. I'm using Xamarin.Google.Android.Material 1.10.0.3.

I'm pretty sure it changed from the original implementation a couple of years back, but I now can't remember what the old way was.

NavigationBarView.IOnItemSelectedListener

bottomNavigationView!.ItemSelected += BottomNavigationView_ItemSelected!;

private void BottomNavigationView_ItemSelected(object sender, NavigationBarView.ItemSelectedEventArgs e)
{

... navController!.Navigate(e.Item.ItemId, null, navOptions);
}

from androidx.

genis-bosch-ypsomed avatar genis-bosch-ypsomed commented on September 27, 2024

Thank you for your helpful answers. From what I see, we weren't using the Java.Lang.Object which should be the issue. I will try this one and come back for feedback.

from androidx.

gmck avatar gmck commented on September 27, 2024

@genis-bosch-ypsomed

What do you get when you right-click NavigationView.IOnNavigationItemSelectedListener and then go to Definition

Mine has JavaObject.

public interface IOnNavigationItemSelectedListener : IJavaObject, IDisposable, IJavaPeerable
{
    [Register("onNavigationItemSelected", "(Landroid/view/MenuItem;)Z", "GetOnNavigationItemSelected_Landroid_view_MenuItem_Handler:Google.Android.Material.Navigation.NavigationView/IOnNavigationItemSelectedListenerInvoker, Xamarin.Google.Android.Material")]
    bool OnNavigationItemSelected(IMenuItem menuItem);
}

Does your main activity inherit from AppCompatActivity?

from androidx.

gmck avatar gmck commented on September 27, 2024

@genis-bosch-ypsomed

I had a bit of a brain fade last night when I posted the above. The above should have been about NavigationBarView.IOnItemSelectedListener, not NavigationView.

It should have read right-click on NavigationBarView.IOnItemSelectedListener

[Register("com/google/android/material/navigation/NavigationBarView$OnItemSelectedListener", "", "Google.Android.Material.Navigation.NavigationBarView/IOnItemSelectedListenerInvoker")]
public interface IOnItemSelectedListener : IJavaObject, IDisposable, IJavaPeerable
{
    [Register("onNavigationItemSelected", "(Landroid/view/MenuItem;)Z", "GetOnNavigationItemSelected_Landroid_view_MenuItem_Handler:Google.Android.Material.Navigation.NavigationBarView/IOnItemSelectedListenerInvoker, Xamarin.Google.Android.Material")]
    bool OnNavigationItemSelected(IMenuItem p0);
}

If you look at the IOnItemSelectedListenerInvoker below that, you'll see it inherits from Java.Lang.Object. If I remember correctly the change I referred to was introduced in Material 1.4.0

from androidx.

genis-bosch-ypsomed avatar genis-bosch-ypsomed commented on September 27, 2024

Thanks @gmck and @jpobst, both solutions worked. We ended using the ItemSelected event as it allows us to directly work on the handler without the need of a new class. I will close this issue.

from androidx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.