简体   繁体   中英

Xamarin forms, App running on Visual Studio Android Emulator But crashes on real device

I have created an app,when build the app runs perfect on Visual Studio emulator. So I created release built and tried running the app on my physical android device. I am getting following error message on Device. I have used Xam.plugin.media

android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0006f] in :0 at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage () [0x0000c] in :0 at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication (Xamarin.Forms.Application application) [0x0025c] in :0 at InterestMatch.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00033] in <9e2a066af7c24de6828dc3a0f4e22d31>:0 at Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <1e89c43d3c7d48b5ba69bfc0b1bdf114>:0 at (wrapper dynamic-method) System.Object:6b534aff-3541-46f2-a92e-ac0022b9c4a7 (intptr,intptr,intptr) at md5ed93386354f0e17f050b2885712023ca.MainActivity.n_onCreate(Native Method) at md5ed93386354f0e17f050b2885 712023ca.MainActivity.onCreate(MainActivity.java:29) at android.app.Activity.performCreate(Activity.java:6303) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509) at android.app.ActivityThread.access$1000(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:5523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

Here is my code

MainActivity.cs

public class MainActivity global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;



        Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }



    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
    {
        PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}

app.xaml.cs

     using InterestMatch.Inter;
     using InterestMatch.Models;
     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using Xamarin.Auth;
     using Xamarin.Forms;

     namespace InterestM
     {
     public partial class App : Application
{

    public static string AppName { get { return "StoreAccountInfoApp"; } }

    public static ICredentialsService CredentialsService { get; private set; }

    public App ()
    {                      
         var account = AccountStore.Create().FindAccountsForService(App.AppName).FirstOrDefault();

         CredentialsService = new CredentialsService();

        if (CredentialsService.DoCredentialsExist())
        {
            InitializeComponent();               
            MainPage = new NavigationPage(new InterestM.MainMenu(account.Username.ToString()));
        }
        else
        {
            InitializeComponent();
            MainPage = new NavigationPage(new InterestM.Accounts.Login());
        }

    }

    protected override void OnStart ()
    {
        // Handle when your app starts
    }

    protected override void OnSleep ()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume ()
    {
        // Handle when your app resumes
    }
}

}

Changing Android project options fixed the issue for me.

Open your Android project's Properties. Go to Android Options. Change Linking property to "Sdk Assemblies Only" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM