-4

i have been trying to display the list of installed apps on click of a button but the package manager is throwing a null pointer exception

this is my java class which is invoked on click

import android.app.ListActivity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class AppList extends AppCompatActivity {

PackageManager packman = getPackageManager();
List<ApplicationInfo> apps = packman.getInstalledApplications(0);
ListView applist;
List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_list);

    applist = (ListView)findViewById(R.id.listView);

    for(ApplicationInfo app : apps) {
        //checks for flags; if flagged, check if updated system app
        if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
            installedApps.add(app);
            //it's a system app, not interested
        } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            //Discard this one
            //in this case, it should be a user-installed app
        } else {
            installedApps.add(app);
        }
    }


    CustomAdapter custom = new CustomAdapter();

    applist.setAdapter(custom);


}

class CustomAdapter extends BaseAdapter
{

    @Override
    public int getCount() {
        return installedApps.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = getLayoutInflater().inflate(R.layout.single_row,null);
        ImageView app_face = (ImageView)convertView.findViewById(R.id.app_face);
        TextView app_name = (TextView)convertView.findViewById(R.id.app_name);

        Drawable face = packman.getApplicationIcon(apps.get(position));
        String label = (String)packman.getApplicationLabel(apps.get(position));

        app_face.setImageDrawable(face);
        app_name.setText(label);

        return convertView;
    }
}
}

and this the error i got while running the app

enter code here

FATAL EXCEPTION: main

Process: com.example.limat.applocker, PID: 17438

java.lang.RuntimeException: Unable to instantiate activity 

Unable to instantiate activity ComponentInfo{com.example.limat.applocker/com.example.limat.applocker.AppList}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference


at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)

at android.app.ActivityThread.-wrap12(ActivityThread.java)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:6119)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference

at com.example.limat.applocker.AppList.<init>(AppList.java:23)

at java.lang.Class.newInstance(Native Method)

at android.app.Instrumentation.newActivity(Instrumentation.java:1078)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 

at android.app.ActivityThread.-wrap12(ActivityThread.java) 

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 

at android.os.Handler.dispatchMessage(Handler.java:102) 

at android.os.Looper.loop(Looper.java:154) 

at android.app.ActivityThread.main(ActivityThread.java:6119) 

at java.lang.reflect.Method.invoke(Native Method) 

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) "
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

Initialise the package manager inside activity life cycle as the context is available there.As the getPackageManager is always called on the context, the earlier code would have resulted in null context

You can call this statement in onCreate() method

packman = getPackageManager();
Nitin Mesta
  • 1,504
  • 19
  • 32
0

Put the following lines of code in onCreate Method

PackageManager packman = getPackageManager();
List<ApplicationInfo> apps = packman.getInstalledApplications(0);
ListView applist;
List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();

You are trying to initialize these things before onCreate method.Refer activity Lifecycle :https://developer.android.com/guide/components/activities/activity-lifecycle.html

Akshay
  • 1,161
  • 1
  • 12
  • 33