我的片段中的ListPopupWindow给出了Java.Lang.NullPointerException错误

时间:2016-06-17 22:54:50

标签: c# android .net xamarin xamarin.android

我有一个带有片段的视图寻呼机,所以我可以左右滑动来切换片段。从其中一个片段内部,我想显示一个弹出窗口。我假设这需要是一个ListPopupWindow。我有一个EditText字段,他们可以输入一些数据。然后我检查条目,如果有一个匹配,那就很好。但是,如果有多个匹配,我想显示一个弹出窗口,以便他们可以选择哪一个是正确的结果。这就是我假设它是ListPopupWindow的原因,我确实需要在弹出窗口之前动态填充列表。我思索是否应该只是在列表视图上滑动一个新片段而不是弹出窗口。但是,我正在尝试使用ListPopupWindow执行此操作,但收到Java.Lang.NullPointerException错误。这是我到目前为止的代码,这段代码在一个可以随时调用的方法中。

            ListPopupWindow popup = new ListPopupWindow(context);  //context is passed in during onCreate of this fragment

        List<string> listForPopup = new List<string>(); //Create a new list to show on the popup for the user to select

        foreach (var c in validationResults) //Iterate through each of the possible results that the user must select
        {
            string name = lookupCourier(c.Item1); //The results only have an ID number, call lookup method to get the name from the ID.
            listForPopup.Add(name); //Add to the new list.
        }

        var adapter = new ArrayAdapter<string>(context, Android.Resource.Layout.SimpleListItem1, listForPopup); //Array adapter from the list.

        popup.SetAdapter(adapter); //Set the adapter to the popup list.
        popup.Width = 300; //Set the width of the popup.
        popup.Height = WindowManagerLayoutParams.WrapContent; //Have the popup window wrap the content. May have one or many to select from. Are there scroll bars?
        popup.Modal = true;
        popup.ItemClick += Popup_ItemClick; //Event handler to fire when the user selects the one item in the list.


        popup.Show(); //Show popup. NOTE: This is there I get the null excpetion.

这是异常的堆栈跟踪(我认为有倍数):

 06-17 22:50:59.674 E/mono    ( 4198): 
06-17 22:50:59.674 E/mono    ( 4198): Unhandled Exception:
06-17 22:50:59.674 E/mono    ( 4198): Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
06-17 22:50:59.674 E/mono    ( 4198):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
06-17 22:50:59.674 E/mono    ( 4198):   at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00084] in /Users/builder/data/lanes/2923/52635947/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:11643 
06-17 22:50:59.674 E/mono    ( 4198):   at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00031] in /Users/builder/data/lanes/2923/52635947/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:26 
06-17 22:50:59.674 E/mono    ( 4198):   at Android.Widget.ListPopupWindow.Show () [0x00000] in /Users/builder/data/lanes/2923/52635947/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Widget.ListPopupWindow.cs:1527 
06-17 22:50:59.674 E/mono    ( 4198):   at PocketPacTracAndroid.Track.popupToSelectCourier (System.Collections.Generic.List`1 validationResults) [0x000a4] in C:\Projects\PocketPacTracAndroid\PocketPacTracAndroid\PocketPacTracAndroid\Track.cs:225 
06-17 22:50:59.674 E/mono    ( 4198):   at PocketPacTracAndroid.Track.validateBarcode (System.String rawBarcode) [0x0014e] in C:\Projects\PocketPacTracAndroid\PocketPacTracAndroid\PocketPacTracAndroid\Track.cs:192 
06-17 22:50:59.674 E/mono    ( 4198):   at PocketPacTracAndroid.Track.Barcode_KeyPress (System.Object sender, Android.Views.KeyEventArgs e) [0x0001c] in C:\Projects\PocketPacTracAndroid\PocketPacTracAndroid\PocketPacTracAndroid\Track.cs:88 
06-17 22:50:59.674 E/mono    ( 4198):   at Android.Views.View+IOnKeyListenerImplementor.OnKey (Android.Views.View v, Keycode keyCode, Android.Views.KeyEvent e) [0x00018] in /Users/builder/data/lanes/2923/52635947/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.View.cs:2854 
06-17 22:50:59.674 E/mono    ( 4198):   at Android.Views.View+IOnKeyListenerInvoker.n_OnKey_Landroid_view_View_ILandroid_view_KeyEvent_ (IntPtr jnienv, IntPtr native__this, IntPtr native_v, Int32 native_keyCode, IntPtr native_e) [0x0001c] in /Users/builder/data/lanes/2923/52635947/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.View.cs:2787 
06-17 22:50:59.674 E/mono    ( 4198):   at (wrapper dynamic-method) System.Object:22b2513e-fc1d-45f2-938f-fbc85af3219a (intptr,intptr,intptr,int,intptr)
06-17 22:50:59.674 E/mono    ( 4198):   --- End of managed exception stack trace ---
06-17 22:50:59.674 E/mono    ( 4198): java.lang.NullPointerException
06-17 22:50:59.674 E/mono    ( 4198):   at android.widget.PopupWindow.getMaxAvailableHeight(PopupWindow.java:1219)
06-17 22:50:59.674 E/mono    ( 4198):   at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1090)
06-17 22:50:59.674 E/mono    ( 4198):   at android.widget.ListPopupWindow.show(ListPopupWindow.java:524)
06-17 22:50:59.674 E/mono    ( 4198):   at mono.android.view.View_OnKeyListenerImplementor.n_onKey(Native Method)
06-17 22:50:59.674 E/mono    ( 4198):   at mono.android.view.View_OnKeyListenerImplementor.onKey(View_OnKeyListenerImplementor.java:30)
06-17 22:50:59.674 E/mono    ( 4198):   at android.view.View.dispatchKeyEvent(View.java:7081)
06-17 22:50:59.674 E/mono    ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono    ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono    ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono    ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono    ( 4198):   at android.support.v4.view.ViewPager.dispatchKeyEvent(ViewPager.java:2567)
06-17 22:50:59.674 E/mono    ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono    ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono    ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono    ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono    ( 4198):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1892)
06-17 22:50:59.674 E/mono    ( 4198):   at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1369)
06-17 22:50:59.674 E/mono    ( 4198):   at android.app.Activity.dispatchKeyEvent(Activity.java:2356)
06-17 22:50:59.674 E/mono    ( 4198):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1819)
06-17 22:50:59.674 E/mono    ( 4198):   at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3575)
06-17 22:50:59.674 E/mono    ( 4198):   at android.view.ViewRoot
06-17 22:50:59.674 E/mono-rt ( 4198): [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
06-17 22:50:59.674 E/mono-rt ( 4198):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2923/52635947/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
06-17 22:50:59.674 E/mono-rt ( 4198):   at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00084] in /Users/builder/data/lanes/2923/52635947/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:11643 
06-17 22:50:59.674 E/mono-rt ( 4198):   at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00031] in /Users/builder/data/lanes/2923/52635947/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:26 
06-17 22:50:59.674 E/mono-rt ( 4198):   at Android.Widget.ListPopupWindow.Show () [0x00000] in /Users/builder/data/lanes/2923/52635947/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Widget.ListPopupWindow.cs:1527 
06-17 22:50:59.674 E/mono-rt ( 4198):   at PocketPacTracAndroid.Track.popupToSelectCourier (System.Collections.Generic.List`1 validationResults) [0x000a4] in C:\Projects\PocketPacTracAndroid\PocketPacTracAndroid\PocketPacTracAndroid\Track.cs:225 
06-17 22:50:59.674 E/mono-rt ( 4198):   at PocketPacTracAndroid.Track.validateBarcode (System.String rawBarcode) [0x0014e] in C:\Projects\PocketPacTracAndroid\PocketPacTracAndroid\PocketPacTracAndroid\Track.cs:192 
06-17 22:50:59.674 E/mono-rt ( 4198):   at PocketPacTracAndroid.Track.Barcode_KeyPress (System.Object sender, Android.Views.KeyEventArgs e) [0x0001c] in C:\Projects\PocketPacTracAndroid\PocketPacTracAndroid\PocketPacTracAndroid\Track.cs:88 
06-17 22:50:59.674 E/mono-rt ( 4198):   at Android.Views.View+IOnKeyListenerImplementor.OnKey (Android.Views.View v, Keycode keyCode, Android.Views.KeyEvent e) [0x00018] in /Users/builder/data/lanes/2923/52635947/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.View.cs:2854 
06-17 22:50:59.674 E/mono-rt ( 4198):   at Android.Views.View+IOnKeyListenerInvoker.n_OnKey_Landroid_view_View_ILandroid_view_KeyEvent_ (IntPtr jnienv, IntPtr native__this, IntPtr native_v, Int32 native_keyCode, IntPtr native_e) [0x0001c] in /Users/builder/data/lanes/2923/52635947/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.View.cs:2787 
06-17 22:50:59.674 E/mono-rt ( 4198):   at (wrapper dynamic-method) System.Object:22b2513e-fc1d-45f2-938f-fbc85af3219a (intptr,intptr,intptr,int,intptr)
06-17 22:50:59.674 E/mono-rt ( 4198):   --- End of managed exception stack trace ---
06-17 22:50:59.674 E/mono-rt ( 4198): java.lang.NullPointerException
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.widget.PopupWindow.getMaxAvailableHeight(PopupWindow.java:1219)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1090)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.widget.ListPopupWindow.show(ListPopupWindow.java:524)
06-17 22:50:59.674 E/mono-rt ( 4198):   at mono.android.view.View_OnKeyListenerImplementor.n_onKey(Native Method)
06-17 22:50:59.674 E/mono-rt ( 4198):   at mono.android.view.View_OnKeyListenerImplementor.onKey(View_OnKeyListenerImplementor.java:30)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.view.View.dispatchKeyEvent(View.java:7081)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.support.v4.view.ViewPager.dispatchKeyEvent(ViewPager.java:2567)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1358)
06-17 22:50:59.674 E/mono-rt ( 4198):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1892)
06-17 22:50:59.674 E/mono-rt ( 4198):   at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1369)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.app.Activity.dispatchKeyEvent(Activity.java:2356)
06-17 22:50:59.674 E/mono-rt ( 4198):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1819)
06-17 22:50:59.674 E/mono-rt ( 4198):   at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3575)
06-17 22:50:59.674 E/mono-rt ( 4198):   at andro

如果有人可以帮助我理解这个错误是什么以及如何修复它(即从我的片段中获取弹出窗口),这将是值得赞赏的。我也愿意接受其他建议来实现我的目标。

谢谢!

1 个答案:

答案 0 :(得分:0)

您需要将锚点设置为ListPopupWindow。你可以将任何视图设置为这样的锚 -

popup.setAnchorView(<your_view_here_to_which_you_want_it_to_be_anchored>);