0

我将 Apache Felix 嵌入到 Android 应用程序中。在该应用程序中,我正在尝试使用捆绑提供的服务。捆绑包已成功安装并启动,但在使用以下服务的行:

 System.out.println( ((AndroidService) services [0]).startActivity());

我收到以下错误:

 java.lang.ClassCastException: androidapi_bundle.AndroidServiceImpl cannot be cast to com.example.android_services.AndroidService

我的捆绑包中有以下内容:

1- 活化剂类

package androidapi_bundle;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import com.example.android_services.AndroidService;
import android.util.Log;


public class Activator implements BundleActivator {

    private static BundleContext context;

    static BundleContext getContext() {
        return context;
    }


    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;

        System.out.println("This is a java message inside the start method of AndroidAPI_Bundle");
        Log.d("Zitona Log","This is android message inside the start method of AndroidAPI_Bundle");


        context.registerService(
                AndroidService.class.getName(), new AndroidServiceImpl(),null);
    }


    public void stop(BundleContext bundleContext) throws Exception {
        Activator.context = null;

        Log.d("Zitona Log","AndroidAPI_Bundle stopped!");
    }

}

2- com.example.android_services 具有AndroidService.java接口:

package com.example.android_services;

public interface AndroidService {

    public String startActivity();


}

3-它的实现类AndroidServiceImpl.java

package androidapi_bundle;

import com.example.android_services.*;

public class AndroidServiceImpl implements AndroidService {

    @Override
    public String startActivity()
    {
        return "Activity started";

    }


}

现在,在我的 android 应用程序中,我也有AndroidService.java界面,以下是使用该服务的代码:

@SuppressWarnings({ "rawtypes", "unchecked" })

ServiceTracker m_tracker = new ServiceTracker(
        m_activator.getContext(),AndroidService.class.getName(), null);

    m_tracker.open();

    System.out.println("8");

    Object[] services = m_tracker.getServices();

    System.out.println("9");
    System.out.println( ((AndroidService) services [0]).startActivity());


   System.out.println("10");

显示“9”后出现错误。我哪里做错了?

以下是我的捆绑清单:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: AndroidAPI_Bundle
Bundle-SymbolicName: AndroidAPI_Bundle
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: androidapi_bundle.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.3.0", android.util, com.example.android_services
Export-Package: com.example.android_services

更新:

我意识到我有这行代码:

   m_configMap.put(FelixConstants.FRAMEWORK_SYSTEMCAPABILITIES_EXTRA, "com.example.android_services");

所以我将其更改为:

    m_configMap.put(FelixConstants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "com.example.android_services");

现在我收到此错误:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apache_felix_android/com.example.apache_felix_android.MainActivity}: java.lang.NullPointerException
4

1 回答 1

-1

获得此信息的唯一方法是,激活程序包和应用程序包都具有服务类的内部副本并且都不导出它。因此,您将需要显示您的清单和捆绑内容。

更新当您似乎遇到非常基本的问题时,您到底在使用诸如系统包之类的高级选项(然后使用 Felix 类而不是 OSGi 的常量类)。为什么不从一个干净简单的 bndtools 开始,使用 IDE 让您的应用程序在其中运行,然后当它运行时将其移植到 Android?这一切看起来都是非常自虐的学习 OSGi 的方式?

于 2013-08-19T14:04:46.753 回答