我刚刚在我的 android 小部件中添加了一个搜索功能,以搜索用户安装的应用程序列表。我的应用程序安装得很好,但是当我去搜索应用程序强制关闭的东西时,我收到了这个错误:
10-18 10:08:39.404: E/AndroidRuntime(8965): FATAL EXCEPTION: main
10-18 10:08:39.404: E/AndroidRuntime(8965): java.lang.NullPointerException
10-18 10:08:39.404: E/AndroidRuntime(8965): at com.example.awesomefilebuilderwidget.Drag_and_Drop_App$2.onTextChanged(Drag_and_Drop_App.java:61)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.widget.TextView.sendOnTextChanged(TextView.java:6576)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.widget.TextView.handleTextChanged(TextView.java:6785)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:7001)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:679)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:185)
10-18 10:08:39.404: E/AndroidRuntime(8965): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
10-18 10:08:39.404: E/AndroidRuntime(8965): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:332)
10-18 10:08:39.404: E/AndroidRuntime(8965): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:86)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.os.Looper.loop(Looper.java:150)
10-18 10:08:39.404: E/AndroidRuntime(8965): at android.app.ActivityThread.main(ActivityThread.java:4333)
10-18 10:08:39.404: E/AndroidRuntime(8965): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 10:08:39.404: E/AndroidRuntime(8965): at java.lang.reflect.Method.invoke(Method.java:507)
10-18 10:08:39.404: E/AndroidRuntime(8965): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-18 10:08:39.404: E/AndroidRuntime(8965): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-18 10:08:39.404: E/AndroidRuntime(8965): at dalvik.system.NativeStart.main(Native Method)
这是 Drag_and_Drop_App.java:
package com.example.awesomefilebuilderwidget;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class Drag_and_Drop_App extends Activity {
private ListView mListAppInfo;
// Search EditText
EditText inputSearch;
AppInfoAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.drag_and_drop_app);
// import buttons
Button btnLinkToFeedback = (Button) findViewById(R.id.btnLinkToFeedback);
// Link to Feedback Screen
btnLinkToFeedback.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
Feedback.class);
startActivity(i);
finish();
}
});
// create new adapter
@SuppressWarnings("unchecked")
AppInfoAdapter adapter = new AppInfoAdapter(this, (List<ApplicationInfo>) Utilities.getInstalledApplication(this), getPackageManager());
// load list application
mListAppInfo = (ListView)findViewById(R.id.lvApps);
// set adapter to list view
mListAppInfo.setAdapter(adapter);
// search bar
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
// Drag_and_Drop_App.this.adapter.getFilter().filter(cs);
Drag_and_Drop_App.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
// implement event when an item on list view is selected
mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
}
});
// implement event when an item on list view is selected via long-click for drag and drop
mListAppInfo.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
return true;
}
});
}
}
这是第 61 行:
Drag_and_Drop_App.this.adapter.getFilter().filter(cs);
我刚刚添加了一个过滤器,所以我可能在某个地方搞砸了:
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();
for (ApplicationInfo appInfo : originalListAppInfo) {
String somefield = appInfo.name;
if (somefield.toLowerCase().contains(constraint.toString())) {
myFilteredAppList.add(appInfo);
}
}
results.count = myFilteredAppList.size();
results.values = myFilteredAppList;
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mListAppInfo = (List<ApplicationInfo>)results.values;
notifyDataSetChanged();
}
};
}
return filter;
}
}
我担心的一行是我的过滤器中的这一行:
String somefield = appInfo.name;
if (somefield.toLowerCase().contains(constraint.toString())) {
我不确定应该在 appInfo 中过滤哪个字段。如果有帮助,这里是我的 xml 布局(片段):
<!-- Editext for Search -->
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/Search_applications"
android:inputType="textVisiblePassword"/>
<ListView
android:id="@+id/lvApps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
有什么问题?
添加: