我创建了一个显示地点列表的应用程序,我创建了一个搜索对话框,其中用户将键入edittext,以便他/她找到所需的位置。但是我收到了一个错误,我将在下面发布。
尝试在com.example.ubaldo.myapplication.Place $ 4 $ 1.onClick(Place.java:221)上的空对象引用上调用虚拟方法'android.text.Editable android.widget.EditText.getText()'
这是我的代码中的第221行
GetSearchPlace = dbhelper.getPlaceSearch(placeLocationEditText.getText().toString());
这是我的DatabaseHelper
public List<PlaceModel> getPlaceSearch(String location) {
List<PlaceModel> search = new ArrayList<PlaceModel>();
String selectQuery = "SELECT * FROM listing_place where province_name like '" + location + "'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
PlaceModel pm = new PlaceModel();
pm.setlisting_title(cursor.getString(cursor.getColumnIndex(KEY_PLACE)));
search.add(pm);
}
while (cursor.moveToNext());
}
cursor.close();
return search;
}
这是我的主要活动
placeLocationEditText = (EditText)findViewById(R.id.placelocation);
Button button = (Button) dialog.findViewById(R.id.btnplacesearch);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
PlaceListView = findViewById(R.id.placelayout);
ViewGroup parent = (ViewGroup) PlaceListView.getParent();
parent.removeView(PlaceListView);
PlaceSearchView = getLayoutInflater().inflate(R.layout.searchresult_place, parent, false);
parent.addView(PlaceSearchView);
GetSearchPlace = dbhelper.getPlaceSearch(placeLocationEditText.getText().toString());
lv2 = (ListView) findViewById(R.id.searchplace_list);
lv2.setAdapter(new ViewAdapterSearchPlace());
答案 0 :(得分:2)
替换
placeLocationEditText = (EditText)findViewById(R.id.placelocation);
与
placeLocationEditText = (EditText)dialog.findViewById(R.id.placelocation);