Xamarin表单,在Visual Studio Android Emulator上运行的应用程序但在真实设备

时间:2018-02-17 06:18:56

标签: c# visual-studio xamarin xamarin.forms xamarin.android

我已经创建了一个应用程序,当构建应用程序在Visual Studio模拟器上运行完美时。所以我创建了发布版本并尝试在我的物理android设备上运行应用程序。我在设备上收到以下错误消息。我使用了Xam.plugin.media

  

android.runtime.JavaProxyThrowable:System.NullReferenceException:对象引用未设置为对象的实例     在Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage(Xamarin.Forms.Page page)[0x0006f] in:0     在Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage()[0x0000c] in:0     在Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication(Xamarin.Forms.Application application)[0x0025c] in:0     在InterestMatch.Droid.MainActivity.OnCreate(Android.OS.Bundle bundle)[0x00033] in< 9e2a066af7c24de6828dc3a0f4e22d31>:0     在AndroidSupport.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_(System.IntPtr jnienv,System.IntPtr native__this,System.IntPtr native_savedInstanceState)[0x0000f] in< 1e89c43d3c7d48b5ba69bfc0b1bdf114&gt ;:0     at(wrapper dynamic-method)System.Object:6b534aff-3541-46f2-a92e-ac0022b9c4a7(intptr,intptr,intptr)       at md5ed93386354f0e17f050b2885712023ca.MainActivity.n_onCreate(Native Method)       at md5ed93386354f0e17f050b2885712023ca.MainActivity.onCreate(MainActivity.java:29)       在android.app.Activity.performCreate(Activity.java:6303)       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509)       在android.app.ActivityThread.access $ 1000(ActivityThread.java:153)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1373)       在android.os.Handler.dispatchMessage(Handler.java:102)       在android.os.Looper.loop(Looper.java:154)       在android.app.ActivityThread.main(ActivityThread.java:5523)       at java.lang.reflect.Method.invoke(Native Method)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:739)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

这是我的代码

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
    }
}

}

1 个答案:

答案 0 :(得分:1)

更改Android项目选项为我解决了这个问题。

打开Android项目的属性。转到Android选项。将链接属性更改为“仅限Sdk程序集”