Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

NullPointerException on Android when checking if declared task is running

$
0
0

I am using Xamarin Forms and have updated everything. The code below works great on the iOS Simulator but crashes on the Android Simulator (at least for API 16). I originally just declared the task in my ViewModel and then, when the user clicks a button, I check to see if the task is running or not from my ContentPage. I have also tried assigning null to the task when I declare it but that does not seem to help. I am sure there is something I can do differently but I am not sure what it would be at this point. Below is the task declaration and method, to check if the task is running from the ViewModel, and then the code for the button from the ContentPage. Below that is the exception I am getting. Thanks for the help!

ViewModel

private Task<ServiceResult>     _getFileTask;               //Declaring the task
//private Task<ServiceResult>     _getFileTask = null;      //Tried assigning null

public bool IsTaskRunning() {
        return _getFileTask != null
           && !_getFileTask.IsCompleted
           && !_getFileTask.IsCanceled
           && !_getFileTask.IsFaulted;
}

ContentPage

private async void OnMenuClicked(object sender, EventArgs e) {
        string answer;

        if(true) {                                      //If I remove the call to _viewModel.IsTaskRunning(), then everything works fine on Android
            answer = await DisplayActionSheet("Menu", "Cancel", null, _viewModel.IsTaskRunning() ? "Cancel Download" : null, "Download", "Manage Downloads");
        } else {
            answer = await DisplayActionSheet("Menu", "Cancel", null, "Manage Downloads");
        }
}

Exception

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2271)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1156)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
at android.view.View.measure(View.java:15172)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
})


Viewing all articles
Browse latest Browse all 75885

Trending Articles