1

My app downloads fine and shows no errors but for some reason, when I go to filter the results for my list of installed applications, sometimes it works and sometimes it doesn't. This time, it didn't work so I looked at the LogCat and noticed it said this:

10-20 13:50:01.768: W/Filter(2366): An exception occured during performFiltering()!
10-20 13:50:01.768: W/Filter(2366): java.lang.NullPointerException
10-20 13:50:01.768: W/Filter(2366):     at com.example.awesomefilebuilderwidget.AppInfoAdapter$1.performFiltering(AppInfoAdapter.java:92)
10-20 13:50:01.768: W/Filter(2366):     at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
10-20 13:50:01.768: W/Filter(2366):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 13:50:01.768: W/Filter(2366):     at android.os.Looper.loop(Looper.java:150)
10-20 13:50:01.768: W/Filter(2366):     at android.os.HandlerThread.run(HandlerThread.java:60)
10-20 13:50:02.859: W/Filter(2366): An exception occured during performFiltering()!
10-20 13:50:02.859: W/Filter(2366): java.lang.NullPointerException
10-20 13:50:02.859: W/Filter(2366):     at com.example.awesomefilebuilderwidget.AppInfoAdapter$1.performFiltering(AppInfoAdapter.java:92)
10-20 13:50:02.859: W/Filter(2366):     at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
10-20 13:50:02.859: W/Filter(2366):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 13:50:02.859: W/Filter(2366):     at android.os.Looper.loop(Looper.java:150)
10-20 13:50:02.859: W/Filter(2366):     at android.os.HandlerThread.run(HandlerThread.java:60)

I get the same warning exception error each time I type a character into the search. Here is my AppInfoAdapter.java:

package com.example.awesomefilebuilderwidget;

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

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

public class AppInfoAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private List<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;
private List<ApplicationInfo> originalListAppInfo;
private Filter filter;

public AppInfoAdapter(Context c, List<ApplicationInfo> listApp, PackageManager pm) {
    mContext = c;
    this.originalListAppInfo = this.mListAppInfo = listApp;
    mPackManager = pm;
    }

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

@Override
public Object getItem(int position) {
    return mListAppInfo.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // get the selected entry
    ApplicationInfo entry = (ApplicationInfo) mListAppInfo.get(position);

    // reference to convertView
    View v = convertView;

    // inflate new layout if null
    if(v == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        v = inflater.inflate(R.layout.layout_appinfo, null);
    }

    // load controls from layout resources
    ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
    TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
    TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);

    // set data to display
    ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
    tvAppName.setText(entry.loadLabel(mPackManager));
    tvPkgName.setText(entry.packageName);

    // return view
    return v;
}
@Override
public Filter getFilter() {
    if(filter == null) {
        filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                List<ApplicationInfo> myFilteredAppList = new ArrayList<ApplicationInfo>();
                constraint = constraint.toString().toLowerCase();

                if (constraint.length() == 0) {
                    myFilteredAppList.addAll(originalListAppInfo);

                }

                for (ApplicationInfo appInfo : originalListAppInfo) {
                    String somefield = appInfo.packageName;
                    String name = appInfo.name;
                    if (somefield.toLowerCase().contains(constraint.toString().toLowerCase().toString())
                            ||name.toLowerCase().contains(constraint.toString().toLowerCase().toString())) {
                        myFilteredAppList.add(appInfo);
                    }
                }
                results.count = myFilteredAppList.size();
                results.values = myFilteredAppList;
                return results;
            }


            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                if(results.values != null)
                {
                mListAppInfo = (List<ApplicationInfo>)results.values;
                notifyDataSetChanged();
                }
            }
        };
    }
    return filter;
}

}

The interesting thing is that this doesn't cause the application to force close or anything but all it does it cause the search not to work. Here is line number 92:

||name.toLowerCase().contains(constraint.toString().toLowerCase().toString())) {

What's returning null or not working right? How can I fix this?

1 Answer 1

3

"name" or "constraint" is null. I would say the name is null.

A simple

if ( ... || (name != null && name.toLowerCase().contains(constraint.toString().toLowerCase().toString()))) {

should solve the problem

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.