为什么我的服务无法被识别?

时间:2018-06-03 16:36:56

标签: android service xamarin.android

我创建了一个服务,用我的自定义键盘替换默认键盘...我的自定义键盘的代码工作(我在应用程序中测试)。我将代码包装在服务中并将其部署到Android设备。当我尝试从设备启用我的自定义键盘时,它只是一直崩溃...我使用log cat来获取异常。这是例外:

  

时间设备名称类型PID标记消息06-03 00:03:38.287 Samsung   SM-J327P错误3044 AndroidRuntime java.lang.RuntimeException:无法使用   实例化服务SimpleKeyboard.SimpleKeyboard.Activity1:   java.lang.ClassNotFoundException:没找到类   " SimpleKeyboard.SimpleKeyboard.Activity1"在路径上:DexPathList [[zip   文件   " /data/app/SimpleKeyboard.SimpleKeyboard-1/base.apk"],nativeLibraryDirectories = [/数据/应用/ SimpleKeyboard.SimpleKeyboard-1 / LIB /臂,   /data/app/SimpleKeyboard.SimpleKeyboard-1/base.apk!/lib/armeabi-v7a,   / vendor / lib,/ system / lib]]   android.app.ActivityThread.handleCreateService(ActivityThread.java:3844)     在android.app.ActivityThread.access $ 2100(ActivityThread.java:231)     在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1911)     在android.os.Handler.dispatchMessage(Handler.java:102)at   android.os.Looper.loop(Looper.java:148)at   android.app.ActivityThread.main(ActivityThread.java:7422)at   java.lang.reflect.Method.invoke(Native Method)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

下面发布的是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="SimpleKeyboard.SimpleKeyboard" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application>
        <service android:name=".Activity1" android:label="@string/app_name" android:permission="android.permission.BIND_INPUT_METHOD" android:exported="true">
            <intent-filter>
                <action android:name="android.view.InputMethod" />
            </intent-filter>
            <meta-data android:name="android.view.im" android:resource="@xml/method" />
        </service>
    </application>
</manifest>

这是服务:

public class Activity1 : InputMethodService
    {
        private KeyboardView kv;
        private Keyboard keyboard;
        private bool isCaps = false;

        public Activity1()
        {

        }
        public override void OnInitializeInterface()
        {
            if (IsExternalStorageWritable())
            {

                File appDirectory = new File(Android.OS.Environment.DirectoryDownloads + "/MyPersonalAppFolder");
                File logDirectory = new File(appDirectory + "/log");
                File logFile = new File(logDirectory, "logcat" + Guid.NewGuid() + ".txt");

                // create app folder
                if (!appDirectory.Exists())
                {
                    appDirectory.Mkdirs();
                }

                // create log folder
                if (!logDirectory.Exists())
                {
                    logDirectory.Mkdirs();
                }

                // clear the previous logcat and then write the new one to the file
                try
                {
                    Java.Lang.Process process = Runtime.GetRuntime().Exec("logcat -c");
                    process = Runtime.GetRuntime().Exec("logcat -f " + logFile);
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                }

            }
            else if (isExternalStorageReadable())
            {
                // only readable
            }
            else
            {
                // not accessible
            }
            base.OnInitializeInterface();
        }
        public override void OnCreate()
        {
            if (IsExternalStorageWritable())
            {

                File appDirectory = new File(Android.OS.Environment.DirectoryDownloads + "/MyPersonalAppFolder");
                File logDirectory = new File(appDirectory + "/log");
                File logFile = new File(logDirectory, "logcat" + Guid.NewGuid() + ".txt");

                // create app folder
                if (!appDirectory.Exists())
                {
                    appDirectory.Mkdirs();
                }

                // create log folder
                if (!logDirectory.Exists())
                {
                    logDirectory.Mkdirs();
                }

                // clear the previous logcat and then write the new one to the file
                try
                {
                    Java.Lang.Process process = Runtime.GetRuntime().Exec("logcat -c");
                    process = Runtime.GetRuntime().Exec("logcat -f " + logFile);
                }
                catch (IOException e)
                {
                    e.PrintStackTrace();
                }

            }
            else if (isExternalStorageReadable())
            {
                // only readable
            }
            else
            {
                // not accessible
            }
            base.OnCreate();
        }
        public override View OnCreateInputView()
        {


            keyboard = new Keyboard(this, Resource.Xml.Qwerty);
            View kvl = (View)LayoutInflater.Inflate(Resource.Layout.Keyboard, null);
            kv = kvl.FindViewById<KeyboardView>(Resource.Id.keyboard);
            kv.Keyboard = keyboard;
            kv.OnKeyboardActionListener = new MyKeyboardListener(this); 
            return kv;
            // return null;
        }
        /* Checks if external storage is available for read and write */
        public bool IsExternalStorageWritable()
        {
            string state = Android.OS.Environment.ExternalStorageState;
            if (Android.OS.Environment.MediaMounted.Equals(state))
            {
                return true;
            }
            return false;
        }

        /* Checks if external storage is available to at least read */
        public bool isExternalStorageReadable()
        {
            string state = Android.OS.Environment.ExternalStorageState;
            if (Android.OS.Environment.MediaMounted.Equals(state) ||
                    Android.OS.Environment.MediaMountedReadOnly.Equals(state))
            {
                return true;
            }
            return false;
        }
    }

我做错了什么?

2 个答案:

答案 0 :(得分:0)

  

java.lang.ClassNotFoundException:没找到类

请参阅@ SushiHangover&#39; s answer

解决方法:

您可以清理/重建作为Windows上的解决方法。

答案 1 :(得分:0)

仍然不是100%的问题所在,但是我下载了Xamarin的示例项目表,发现我几乎没有关键区别...添加和添加文件以及该清单中缺少的两行...现在可以使用...对于任何有此问题的人,请查看xamarin服务项目示例,您也应该能够解决您的问题。