1

我的应用程序下载正常并且没有显示错误,但由于某种原因,当我去过滤已安装应用程序列表的结果时,有时它可以工作,有时它不能。这一次,它不起作用,所以我查看了 LogCat 并注意到它是这样说的:

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)

每次在搜索中键入字符时,我都会收到相同的警告异常错误。这是我的 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;
}

}

有趣的是,这不会导致应用程序强制关闭或任何事情,但它所做的一切都会导致搜索不起作用。这是第 92 行:

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

什么返回 null 或无法正常工作?我怎样才能解决这个问题?

4

1 回答 1

3

“名称”或“约束”为空。我会说这个名字是空的。

一个简单的

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

应该解决问题

于 2013-10-20T20:00:27.947 回答