I am embedding Apache Felix into an Android application. In that application, I am trying to use a service provided by a bundle. The bundle is successfully installed and started, but at the line that uses the service which is:
System.out.println( ((AndroidService) services [0]).startActivity());
I get the following error:
java.lang.ClassCastException: androidapi_bundle.AndroidServiceImpl cannot be cast to com.example.android_services.AndroidService
I have the following in my bundle:
1- Activator class
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 which has AndroidService.java
interface:
package com.example.android_services;
public interface AndroidService {
public String startActivity();
}
3- its implementation class AndroidServiceImpl.java
:
package androidapi_bundle;
import com.example.android_services.*;
public class AndroidServiceImpl implements AndroidService {
@Override
public String startActivity()
{
return "Activity started";
}
}
Now, in my android app I also have AndroidService.java
interface, and the following is the code that uses the service:
@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");
After "9" is displayed I get the error. Where did I go wrong?
Below is my bundle MANIFEST:
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
Update:
I realized that I have this line of code:
m_configMap.put(FelixConstants.FRAMEWORK_SYSTEMCAPABILITIES_EXTRA, "com.example.android_services");
so I changed it to:
m_configMap.put(FelixConstants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "com.example.android_services");
Now I am getting this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apache_felix_android/com.example.apache_felix_android.MainActivity}: java.lang.NullPointerException