我的应用程序下载正常并且没有显示任何错误,但出于某种原因,当我过滤我已安装的应用程序列表的结果时,有时它会起作用,有时它却不起作用。这一次,它没有用,所以我看了看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或不能正常工作?我该如何解决这个问题?
答案 0 :(得分:2)
“name”或“constraint”为空。我会说这个名字是空的。
一个简单的
if ( ... || (name != null && name.toLowerCase().contains(constraint.toString().toLowerCase().toString()))) {
应解决问题