Why am I seeing this error for getPackageManager()?

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. :slight_smile:

hello i am not so advanced in java but i think instead of

we need to write

@SimpleFunction(description = "Launches the application.")
public void openApp(final String packageName) {
    if(packageName != null){
        PackageManager pm = content.pm.PackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage(packageName);
        context.startActivity(launchIntent);
    }
}

You haven’t initialized context.

3 Likes

Exactly.
Use container.$context(); to get context.

3 Likes

@Helper @WatermelonIce @luv.ak.tech
Thanks. Let me try all these solutions. :slight_smile:

1 Like

It’s not working.

@WatermelonIce @Helper, I’m still getting same error after initializing the context.

1 Like

share your current code

1 Like

Please use modify your original code but not @luv.ak.tech. His solution doesn’t help at all, unfortunately.

3 Likes

You still use context.getPackageManager().

2 Likes

Have you initialized it in this way?

If so then you are ignoring the result.
Do it like this:
context = container.$context();

2 Likes

Yeah. Because when I use context.pm.getPackageManager(), the editor throws an error.

1 Like

Yup. Unfortunately, I get error while using @luv.ak.tech code.

Let me try. :slight_smile:
Update:
It is working now. Thanks for the help.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.