I tried to make an extension in Appybuilder editor. Here’s the code:
import android.content.Context;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import com.google.appinventor.components.annotations.;
import com.google.appinventor.components.runtime.;
import com.google.appinventor.components.common.ComponentCategory;
@DesignerComponent(version = 1, description = “This Extension was created with the AppyBuilder Code Editor.
” +
“Create your own here:
https://editor.appybuilder.com
”,
category = ComponentCategory.EXTENSION,
nonVisible = true, iconName = “http://appyBuilder.com/extensions/icons/extension.png”)
@SimpleObject(external = true)
public class AppLauncher extends AndroidNonvisibleComponent {
PackageManager pm;
public Activity activity;
public Context context;
private ComponentContainer container;
/**
* @param container container, component will be placed in
*/
public AppLauncher(ComponentContainer container) {
super(container.$form());
this.container = container;
}
@SimpleFunction(description = "Launches the application.")
public void openApp(final String packageName) {
if(packageName != null){
PackageManager pm = context.getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage(packageName);
context.startActivity(launchIntent);
}
}
}
I compiled this code and got my extension. I used this extension in my app to launch application but got this error:
Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
Please help me in resolving this error. Thank you.