C#Xamarin:从DLL调用时,工作代码会抛出NullReference Exception

发布于 2025-02-09 02:34:22 字数 4819 浏览 2 评论 0原文

因此,我有此代码段,它为我的Xamarin表单创建了一个用 +和 - 按钮的整数控件。当我将其直接以Xamarin形式放置时,这就像魅力一样。但是,一旦我将其放入DLL上的课程以进行重复使用,我就会在单击“ Plus”或“减去”时获得NullReferenceException。没有办法捕获此例外或处理任何无效案例(正如我尝试过的正常案例所见),NullReFerenceException似乎正在我无法控制的空间中发生。

        public View createIntegerControl(String name, String value, int DatabaseId = 0)
    {
        StackLayout newStackLayout = new StackLayout();
        newStackLayout.SetValue(AttachableProperties.DatabaseIdProperty, DatabaseId);

        Label label = new Label();
        label.Text = name;

        Grid inputFields = new Grid();

        ColumnDefinition col1 = new ColumnDefinition();
        ColumnDefinition col2 = new ColumnDefinition();
        ColumnDefinition col3 = new ColumnDefinition();

        GridLength gridLengthEntry = new GridLength(70, GridUnitType.Star);
        GridLength gridLengthButton = new GridLength(15, GridUnitType.Star);

        col1.Width = gridLengthEntry;
        col2.Width = gridLengthButton;
        col3.Width = gridLengthButton;

        inputFields.ColumnDefinitions.Add(col1);
        inputFields.ColumnDefinitions.Add(col2);
        inputFields.ColumnDefinitions.Add(col3);

        Entry entry = new Entry();
        entry.Text = value;
        entry.SetValue(Grid.ColumnProperty, 0);

        Button plus = new Button();
        plus.Text = "+";
        plus.SetValue(Grid.ColumnProperty, 1);
        plus.Clicked += plusClicked;

        Button minus = new Button();
        minus.Text = "-";
        minus.SetValue(Grid.ColumnProperty, 2);
        minus.Clicked += minusClicked;

        newStackLayout.Children.Add(label);
        inputFields.Children.Add(entry);
        inputFields.Children.Add(plus);
        inputFields.Children.Add(minus);
        newStackLayout.Children.Add(inputFields);

        return newStackLayout;
    }

public static void plusClicked(object sender, EventArgs e)
    {
        try
        {
            if (sender != null)
            {
                Button button = (Button)sender;
                if (button.Parent != null)
                {
                    Grid grid = (Grid)button.Parent;

                    if (grid.Parent != null)
                    {
                        if (grid.Children.Count > 0)
                        {
                            Entry entry = (Entry)grid.Children[0];
                            entry.Text = (Convert.ToInt32(entry.Text) + 1).ToString();
                        }
                        else Console.WriteLine("no children for grid");
                    }
                    else Console.WriteLine("no grid parent");
                }
                else Console.WriteLine("no button parent");
            }
            else Console.WriteLine("no sender");
        }
        catch
        {
            Console.WriteLine("Exception");
        }
    }

    public void minusClicked(object sender, EventArgs e)
    {
        if (sender != null)
        {
            Button button = (Button)sender;
            Grid grid = (Grid)button.Parent;
            Entry entry = (Entry)grid.Children[0];
            entry.Text = (Convert.ToInt32(entry.Text) - 1).ToString();
        }
    }

工人线程的其他数据

    Android.Runtime.JNINativeWrapper._unhandled_exception(System.TypeLoadException e) Line 12
Nicht markiert  >   1   0   Arbeitsthread       Android.Runtime.JNINativeWrapper._unhandled_exception(System.TypeLoadException e) Line 12
0xE in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5
0x1E in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:111,26
0x11 in Xamarin.Forms.Button.Xamarin.Forms.Internals.IButtonElement.PropagateUpClicked at D:\a\1\s\Xamarin.Forms.Core\Button.cs:187,47
0x20 in Xamarin.Forms.ButtonElement.ElementClicked at D:\a\1\s\Xamarin.Forms.Core\ButtonElement.cs:61,5
0x2 in Xamarin.Forms.Button.SendClicked at D:\a\1\s\Xamarin.Forms.Core\Button.cs:173,32
0x4 in Xamarin.Forms.Platform.Android.ButtonElementManager.OnClick at D:\a\1\s\Xamarin.Forms.Platform.Android\ButtonElementManager.cs:25,4
0xD in Xamarin.Forms.Platform.Android.FastRenderers.ButtonRenderer.Android.Views.View.IOnClickListener.OnClick at D:\a\1\s\Xamarin.Forms.Platform.Android\FastRenderers\ButtonRenderer.cs:72,45
0x11 in Android.Views.View.IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-31/mcw/Android.Views.View.cs:2280,5
0x9 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:110

so I have this code segment, it creates an integer control for my Xamarin form with an accompaning + and - button. This works like a charm when I put it directly in my Xamarin form. But as soon as I put it into a class in a DLL for reusing, I get NullReferenceException, when clicking "plus" or "minus". There's no way to catch this Exception or handle any null case (as you see in plusClicked I tried), the NullReferenceException seems to be happening in a space out of my control.

        public View createIntegerControl(String name, String value, int DatabaseId = 0)
    {
        StackLayout newStackLayout = new StackLayout();
        newStackLayout.SetValue(AttachableProperties.DatabaseIdProperty, DatabaseId);

        Label label = new Label();
        label.Text = name;

        Grid inputFields = new Grid();

        ColumnDefinition col1 = new ColumnDefinition();
        ColumnDefinition col2 = new ColumnDefinition();
        ColumnDefinition col3 = new ColumnDefinition();

        GridLength gridLengthEntry = new GridLength(70, GridUnitType.Star);
        GridLength gridLengthButton = new GridLength(15, GridUnitType.Star);

        col1.Width = gridLengthEntry;
        col2.Width = gridLengthButton;
        col3.Width = gridLengthButton;

        inputFields.ColumnDefinitions.Add(col1);
        inputFields.ColumnDefinitions.Add(col2);
        inputFields.ColumnDefinitions.Add(col3);

        Entry entry = new Entry();
        entry.Text = value;
        entry.SetValue(Grid.ColumnProperty, 0);

        Button plus = new Button();
        plus.Text = "+";
        plus.SetValue(Grid.ColumnProperty, 1);
        plus.Clicked += plusClicked;

        Button minus = new Button();
        minus.Text = "-";
        minus.SetValue(Grid.ColumnProperty, 2);
        minus.Clicked += minusClicked;

        newStackLayout.Children.Add(label);
        inputFields.Children.Add(entry);
        inputFields.Children.Add(plus);
        inputFields.Children.Add(minus);
        newStackLayout.Children.Add(inputFields);

        return newStackLayout;
    }

public static void plusClicked(object sender, EventArgs e)
    {
        try
        {
            if (sender != null)
            {
                Button button = (Button)sender;
                if (button.Parent != null)
                {
                    Grid grid = (Grid)button.Parent;

                    if (grid.Parent != null)
                    {
                        if (grid.Children.Count > 0)
                        {
                            Entry entry = (Entry)grid.Children[0];
                            entry.Text = (Convert.ToInt32(entry.Text) + 1).ToString();
                        }
                        else Console.WriteLine("no children for grid");
                    }
                    else Console.WriteLine("no grid parent");
                }
                else Console.WriteLine("no button parent");
            }
            else Console.WriteLine("no sender");
        }
        catch
        {
            Console.WriteLine("Exception");
        }
    }

    public void minusClicked(object sender, EventArgs e)
    {
        if (sender != null)
        {
            Button button = (Button)sender;
            Grid grid = (Grid)button.Parent;
            Entry entry = (Entry)grid.Children[0];
            entry.Text = (Convert.ToInt32(entry.Text) - 1).ToString();
        }
    }

Additional data from the worker thread

    Android.Runtime.JNINativeWrapper._unhandled_exception(System.TypeLoadException e) Line 12
Nicht markiert  >   1   0   Arbeitsthread       Android.Runtime.JNINativeWrapper._unhandled_exception(System.TypeLoadException e) Line 12
0xE in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5
0x1E in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:111,26
0x11 in Xamarin.Forms.Button.Xamarin.Forms.Internals.IButtonElement.PropagateUpClicked at D:\a\1\s\Xamarin.Forms.Core\Button.cs:187,47
0x20 in Xamarin.Forms.ButtonElement.ElementClicked at D:\a\1\s\Xamarin.Forms.Core\ButtonElement.cs:61,5
0x2 in Xamarin.Forms.Button.SendClicked at D:\a\1\s\Xamarin.Forms.Core\Button.cs:173,32
0x4 in Xamarin.Forms.Platform.Android.ButtonElementManager.OnClick at D:\a\1\s\Xamarin.Forms.Platform.Android\ButtonElementManager.cs:25,4
0xD in Xamarin.Forms.Platform.Android.FastRenderers.ButtonRenderer.Android.Views.View.IOnClickListener.OnClick at D:\a\1\s\Xamarin.Forms.Platform.Android\FastRenderers\ButtonRenderer.cs:72,45
0x11 in Android.Views.View.IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-31/mcw/Android.Views.View.cs:2280,5
0x9 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:110

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2025-02-16 02:34:22

虽然我认为这不是问题(鉴于代码直接在您的主要项目中起作用),但请修复您的null检查:

// If isn't a button, result is null.
Button button = sender as Button;
if (button != null)
{
    // If isn't a grid, result is null.
    Grid grid = button.Parent as Grid;
    if (grid != null)
    {
        ...
        // If isn't an Entry, result is null.
        Entry entry = grid.Children[0] as Entry;
        if (entry != null)
        {

While I don't think it is the problem (given that the code works when directly in your main project), fix your null-checks:

// If isn't a button, result is null.
Button button = sender as Button;
if (button != null)
{
    // If isn't a grid, result is null.
    Grid grid = button.Parent as Grid;
    if (grid != null)
    {
        ...
        // If isn't an Entry, result is null.
        Entry entry = grid.Children[0] as Entry;
        if (entry != null)
        {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文