Xamarin Media插件无法在Android

时间:2018-02-02 16:47:02

标签: android xamarin xamarin.android media nullreferenceexception

我已经安装了插件Xam.Plugin.Media。 该插件在IOS上运行良好,但在Andorid上,应用程序会在启动后立即崩溃。

我已经按照安装插件后的readme.txt进行操作,因此我的文件是:

MainActivity.cs:

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using FFImageLoading.Forms.Droid;

namespace SgatMobileV2.Droid {
    [Activity(Label = "SgatMobileV2", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity {
        protected override void OnCreate(Bundle bundle) {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);

            CachedImageRenderer.Init(enableFastRenderer: true); // Inizializza il plugin FFImageLoading

            LoadApplication(new App());
        }

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

资源/ XML / file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-files-path name="my_images" path="Pictures" />
  <external-files-path name="my_movies" path="Movies" />
</paths>

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="it.volos.SgatMobile" android:installLocation="auto">
  <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="26" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.CAMERA" />
  <application android:label="SgatMobileV2.Android" android:icon="@drawable/icon">
    <provider android:name="android.support.v4.content.FileProvider" android:authorities="it.volos.SgatMobile.fileprovider" android:exported="false" android:grantUriPermissions="true">
      <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
    </provider>
  </application>
</manifest>

但在应用启动后,会触发此错误:

System.NullReferenceException: Object reference not set to an instance of an object.

有人可以帮我吗?

谢谢!

4 个答案:

答案 0 :(得分:1)

cleaning the solutionclosing Visual Studiodeleting the obj and bin folders后,我的项目再次正常工作......

我还将 file_paths.xml 的构建操作设置为AndroidResource

我希望这会有所帮助。

答案 1 :(得分:0)

根据插件的documentation,你应该在OnCreate()方法中添加这一行:

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

答案 2 :(得分:0)

我遇到了同样的问题,并从global::Xamarin.Forms.Platform.Android.FormsApplicationActivity而不是global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity继承了MainActivitiy类,解决了我的问题。

不要忘记按照自述文件的说明操作。例如,创建MainApplication.cs文件。

[Application]
public class MainApplication : Application, Application.IActivityLifecycleCallbacks {
    public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer) {
    }

    public override void OnCreate() {
        base.OnCreate();
        RegisterActivityLifecycleCallbacks(this);
        //A great place to initialize Xamarin.Insights and Dependency Services!
    }

    public override void OnTerminate() {
        base.OnTerminate();
        UnregisterActivityLifecycleCallbacks(this);
    }

    public void OnActivityCreated(Activity activity, Bundle savedInstanceState) {
        CrossCurrentActivity.Current.Activity = activity;
    }

    public void OnActivityDestroyed(Activity activity) {
    }

    public void OnActivityPaused(Activity activity) {
    }

    public void OnActivityResumed(Activity activity) {
        CrossCurrentActivity.Current.Activity = activity;
    }

    public void OnActivitySaveInstanceState(Activity activity, Bundle outState) {
    }

    public void OnActivityStarted(Activity activity) {
        CrossCurrentActivity.Current.Activity = activity;
    }

    public void OnActivityStopped(Activity activity) {
    }
}

答案 3 :(得分:0)

我一直收到System.NullReferenceException:对象引用未设置为对象的实例,即使在完成所有步骤之后

我能够根据上面提供的解决方案修复我的问题,而不是继承自global :: Xamarin.Forms.Platform.Android.FormsApplicationActivity而不是global :: Xamarin.Forms.Platform.Android.FormsAppCompatActivity

代码更改示例: 从

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

public class MainActivity :  global::Xamarin.Forms.Platform.Android.FormsApplicationActivity