Xamarin-PrintService项目上的“ java.lang.RuntimeException:无法实例化服务”

时间:2018-07-11 19:27:07

标签: android visual-studio xamarin xamarin.android

我正在尝试使Xamarin PCL Android Project中的PrintService类工作,并使我的PrintService出现在打印选项中,但是在激活它或查找打印机程序时,Visual Studio会显示下一个错误:

void map( @MappingTarget Target.X target, Source.ID source )

我已经在这里,Google等上阅读了很多帖子,但是找不到可行的解决方案。

我正在使用Visual Studio 2017 15.7.4,JDK 8,Xamarin 4.10.10.2。

有解决此错误的想法吗?

AndroidManifest.xml

07-11 18:56:32.802 E/AndroidRuntime( 6088): java.lang.RuntimeException: Unable to instantiate service PrintService.Test.TransferPrintService: java.lang.ClassNotFoundException: Didn't find class "PrintService.Test.TransferPrintService" on path: DexPathList[[zip file "/data/app/PrintService.Test-dAvKeFP8zI2zBEHqn5yyqA==/base.apk"],nativeLibraryDirectories=[/data/app/PrintService.Test-dAvKeFP8zI2zBEHqn5yyqA==/lib/x86, /system/fake-libs, /data/app/PrintService.Test-dAvKeFP8zI2zBEHqn5yyqA==/base.apk!/lib/x86, /system/lib, /vendor/lib]]

MainActivity.cs

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="PrintService.Test" android:installLocation="auto" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="19" />
    <application android:label="@string/app_name" android:allowBackup="false" android:icon="@mipmap/ic_launcher">
        <service android:name=".TransferPrintService" android:permission="android.permission.BIND_PRINT_SERVICE">
            <intent-filter>
                <action android:name="android.printservice.PrintService" />
            </intent-filter>
            <meta-data android:name="android.printservice" android:resource="@xml/transfer_print_service" />
        </service>
    </application>
</manifest>

TransferPrintService.cs

namespace PrintServiceTest2
{
    using Android.App;
    using Android.Widget;
    using Android.OS;
    using Android.Support.V7.App;
    using Android.Content;

    [Activity(Label = "Test Print Service",
        Theme = "@style/AppTheme", MainLauncher = true,
        Name = "PrintService.Test.MainActivity")]
    public class MainActivity : AppCompatActivity
    {
        public static Toast mainToast;
        public static Intent myIntent;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            mainToast = Toast.MakeText(this, "Service", ToastLength.Long);
            myIntent = new Intent("android.printservice.PrintService");

            //var intent = new Intent(this, typeof(TransferPrintService));
            //StartService(intent);
        }

        public static void ShowToast(string text)
        {
            mainToast.SetText(text);
            mainToast.Show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

通过使用Service属性上的Name参数,将导出的Xamarin.Android JNI / Java包装器类(AWC)与手动清单条目相匹配:

[Service(Name = "PrintService.Test.TransferPrintService")]
public class TransferPrintService : PrintService
{

或者不要手动编辑清单,让Xamarin构建它:

[Service(Permission = "android.permission.BIND_PRINT_SERVICE")]
[IntentFilter(new String[] { "android.printservice.PrintService" })]
public class TransferPrintService : PrintService
{
   ~~~
}