1

I am trying to get a ListView of user installed android apps to show up in a fragment in my android app. I am populating its items using custom BaseAdapter.

Since I am using a fragment, I am running into problems.

I had to convert my Activity to Fragment. I did that successfully, but after that I got runtime errors in my Adapter class.

06-23 00:40:53.824  23082-23082/com.spicycurryman.getdisciplined10.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.ibc.android.demo.appslist.app.ApkAdapter.getView(ApkAdapter.java:50)

LayoutInflator is line 50 where is error is

LayoutInflater inflater = (LayoutInflater) convertView.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );

I did some research

NullPointerException in getView Of Adapter extends BaseAdapter

extends BaseAdapter appear NullPointerException

and attempted to follow these; however, the solution still produced the error.

I am not sure why convertView == null is always false.

package com.ibc.android.demo.appslist.app;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.spicycurryman.getdisciplined10.app.InstalledAppActivity;
import com.spicycurryman.getdisciplined10.app.R;

import java.util.List;

public class ApkAdapter extends BaseAdapter {

    List<PackageInfo> packageList;
    InstalledAppActivity context;
    PackageManager packageManager;

    public ApkAdapter(InstalledAppActivity context, List<PackageInfo> packageList,
                      PackageManager packageManager) {
        super();
        this.context = context;
        this.packageList = packageList;
        this.packageManager = packageManager;
    }

    private class ViewHolder {
        TextView apkName;
    }

    public int getCount() {
        return packageList.size();
    }

    public Object getItem(int position) {
        return packageList.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        // LayoutInflator is line 50 where is error is
        LayoutInflater inflater = (LayoutInflater) convertView.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.installed_apps, null);
            holder = new ViewHolder();

            holder.apkName = (TextView) convertView.findViewById(R.id.appname);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PackageInfo packageInfo = (PackageInfo) getItem(position);
        Drawable appIcon = packageManager
                .getApplicationIcon(packageInfo.applicationInfo);
        String appName = packageManager.getApplicationLabel(
                packageInfo.applicationInfo).toString();
        appIcon.setBounds(0, 0, 65, 65);
        holder.apkName.setCompoundDrawables(appIcon, null, null, null);
        holder.apkName.setCompoundDrawablePadding(15);
        holder.apkName.setText(appName);

        return convertView;
    }
}

Here is my Fragment class:

package com.spicycurryman.getdisciplined10.app;

import android.support.v4.app.Fragment;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.ibc.android.demo.appslist.app.ApkAdapter;

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



public class InstalledAppActivity extends Fragment
        implements OnItemClickListener {

    PackageManager packageManager;
    ListView apkList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View rootView = inflater.inflate(R.layout.installed_apps, container, false);
        packageManager = getActivity().getPackageManager();
        List<PackageInfo> packageList = packageManager
                .getInstalledPackages(PackageManager.GET_PERMISSIONS);

        List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();

        /*To filter out System apps*/
        for(PackageInfo pi : packageList) {
            boolean b = isSystemPackage(pi);
            if(!b) {
                packageList1.add(pi);
            }
        }
        apkList = (ListView) rootView.findViewById(R.id.applist);
        apkList.setAdapter(new ApkAdapter(this, packageList1, packageManager));

        apkList.setOnItemClickListener(this);

        return rootView;

    }

    /**
     * Return whether the given PackgeInfo represents a system package or not.
     * User-installed packages (Market or otherwise) should not be denoted as
     * system packages.
     *
     * @param pkgInfo
     * @return boolean
     */
    private boolean isSystemPackage(PackageInfo pkgInfo) {
        return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
                : false;
    }


// Don't need in Fragment
 /*   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.block, menu);
        return true;
    }*/

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}

Please remember that InstalledAppActivity is a fragment.

EDIT:

Here are my updated errors

enter image description here

enter image description here

Community
  • 1
  • 1
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81

2 Answers2

4

Change this

  apkList.setAdapter(new ApkAdapter(this, packageList1, packageManager));

to

 apkList.setAdapter(new ApkAdapter(getActivity(), packageList1, packageManager));

this in Fragment does not refer to valid context.

Also

LayoutInflater inflater; // declare as instance variable

Then in the constructor

public ApkAdapter(Context context, List<PackageInfo> packageList,
                  PackageManager packageManager) {
    super();
    inflater = LayoutInflater.from(context); // can initialize here 
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

You are trying to obtain the context from a view that is null, this is why the app crashes. Modifying the below line in the getView() function will solve the issue

LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
SathMK
  • 1,171
  • 1
  • 8
  • 18