addTextChangedlistener 返回错误,请帮忙。我从另一个xml for Listview有Addportfolio.java文件加载列表对话框。 listview xml有搜索文本和列表视图字段
AddPortfolio.java
import java.util.ArrayList;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class AddPortfolio extends Activity implements OnItemClickListener {
ListView list;
Dialog listDialog;
DBAdapter myDb;
private ArrayAdapter adapter;
ArrayList nameList = new ArrayList();
private ArrayList<String> array_search= new ArrayList<String>();
int textlength=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_portfolio);
myDb = new DBAdapter(this);
myDb.open();
Cursor cursor = myDb.getAllRows();
if (cursor.moveToFirst()) {
do {
// Process the data:
String name = cursor.getString(1);
nameList.add(name);
} while(cursor.moveToNext());
}
// Close the cursor to avoid a resource leak.
cursor.close();
}
public void addlist(View v){
showdialog();
}
private void showdialog()
{
listDialog = new Dialog(AddPortfolio.this);
listDialog.setTitle("Select Company");
LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.listviewdisplay, null, false);
listDialog.setContentView(v);
listDialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
list = (ListView) listDialog.findViewById(R.id.listview);
list.setOnItemClickListener((OnItemClickListener) this);
//list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, nameList));
//now that the dialog is set up, it's time to show it
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, nameList);
list.setAdapter(adapter);
listDialog.show();
EditText st= (EditText) findViewById(R.id.editText1);
st.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
AddPortfolio.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
Log.i("REACHES BEFORE", "YES");
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
EditText edittext1=(EditText) findViewById(R.id.add_company);
edittext1.setText((CharSequence) nameList.get(arg2));
listDialog.cancel();
}
}
Logcat错误
02-19 02:24:26.484: D/AndroidRuntime(1487): Shutting down VM
02-19 02:24:26.484: D/AndroidRuntime(1487): Shutting down VM
02-19 02:24:26.484: W/dalvikvm(1487): threadid=1: thread exiting with uncaught exception (group=0xb3afeba8)
02-19 02:24:26.584: E/AndroidRuntime(1487): FATAL EXCEPTION: main
02-19 02:24:26.584: E/AndroidRuntime(1487): Process: com.example.merostock, PID: 1487
02-19 02:24:26.584: E/AndroidRuntime(1487): java.lang.IllegalStateException: Could not execute method of the activity
02-19 02:24:26.584: E/AndroidRuntime(1487): at android.view.View$1.onClick(View.java:3823)
02-19 02:24:26.584: E/AndroidRuntime(1487): at android.view.View.performClick(View.java:4438)
02-19 02:24:26.584: E/AndroidRuntime(1487): at android.view.View$PerformClick.run(View.java:18422)
02-19 02:24:26.584: E/AndroidRuntime(1487): at android.os.Handler.handleCallback(Handler.java:733)
02-19 02:24:26.584: E/AndroidRuntime(1487): at android.os.Handler.dispatchMessage(Handler.java:95)
02-19 02:24:26.584: E/AndroidRuntime(1487): at android.os.Looper.loop(Looper.java:136)
02-19 02:24:26.584: E/AndroidRuntime(1487): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-19 02:24:26.584: E/AndroidRuntime(1487): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 02:24:26.584: E/AndroidRuntime(1487): at java.lang.reflect.Method.invoke(Method.java:515)
02-19 02:24:26.584: E/AndroidRuntime(1487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-19 02:24:26.584: E/AndroidRuntime(1487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-19 02:24:26.584: E/AndroidRuntime(1487): at dalvik.system.NativeStart.main(Native Method)
02-19 02:24:26.584: E/AndroidRuntime(1487): Caused by: java.lang.reflect.InvocationTargetException
02-19 02:24:26.584: E/AndroidRuntime(1487): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 02:24:26.584: E/AndroidRuntime(1487): at java.lang.reflect.Method.invoke(Method.java:515)
02-19 02:24:26.584: E/AndroidRuntime(1487): at android.view.View$1.onClick(View.java:3818)
02-19 02:24:26.584: E/AndroidRuntime(1487): ... 11 more
02-19 02:24:26.584: E/AndroidRuntime(1487): Caused by: java.lang.NullPointerException
02-19 02:24:26.584: E/AndroidRuntime(1487): at com.example.merostock.AddPortfolio.showdialog(AddPortfolio.java:77)
02-19 02:24:26.584: E/AndroidRuntime(1487): at com.example.merostock.AddPortfolio.addlist(AddPortfolio.java:53)
02-19 02:24:26.584: E/AndroidRuntime(1487): ... 14 more
答案 0 :(得分:0)
请检查AddPortfolio.java中的第77行,它会导致NPE