我将 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