高级搜索栏崩溃

时间:2019-10-20 12:04:20

标签: android nullpointerexception

我正在尝试在我的应用中实施搜索,当我单击搜索图标时,活动崩溃。错误是:https://i.imgur.com/CDOE9fT.png 这是Activity的代码:

public class SearchActivity extends AppCompatActivity implements OnItemClickListener, AdapterView.OnItemClickListener {
DBHelper dbHelper;
SQLiteDatabase database;
ArrayList<Reminder> ReminderList = new ArrayList<>();
ListView listView;
MaterialSearchView searchView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Material search");
    toolbar.setTitleTextColor(Color.parseColor("#FFFFFF"));


    dbHelper = new DBHelper(this);
    database = dbHelper.getWritableDatabase();
    //listView = (ListView) findViewById(R.id.ListOfReminders);
    listView = (ListView)findViewById(R.id.ListOfReminders);

    Cursor cursor = database.query(TABLE_REMINDERS, null, null, null, null, null, null);
    if (cursor.moveToFirst()) {

        do {
            int idIndex = cursor.getInt(cursor.getColumnIndex(DBHelper.KEY_ID));
            String nameIndex = cursor.getString(cursor.getColumnIndex(KEY_NAME));
            String hourIndex = cursor.getString(cursor.getColumnIndex(DBHelper.KEY_HOUR));
            String dateIndex = cursor.getString(cursor.getColumnIndex(DBHelper.KEY_DATE));
            String name = nameIndex;
            String hour = hourIndex;
            String date = dateIndex;

            ReminderList.add(new Reminder(idIndex, name, hour, date));
            ReminderListAdapter2 adapter = new ReminderListAdapter2(this, R.layout.reminder_view2, ReminderList);
            adapter.setListener(this);
            listView.setAdapter(adapter);

        } while (cursor.moveToNext());
    } else
        Log.d("mLog", "0 rows in db");


    searchView = (MaterialSearchView)findViewById(R.id.search_view);
    searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
        @Override
        public void onSearchViewShown() {

        }

        @Override
        public void onSearchViewClosed() {
            listView = (ListView)findViewById(R.id.ListOfReminders);
            ReminderListAdapter2 adapter = new ReminderListAdapter2(SearchActivity.this, android.R.layout.simple_list_item_1,ReminderList);
            listView.setAdapter(adapter);

        }
    }); searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    if(newText != null && !newText.isEmpty()){
        ArrayList<Reminder> lstFound = new ArrayList<>();
        for(Reminder item:ReminderList){
            if(item.getName().contains(newText))
                lstFound.add(new Reminder(item.getId(), item.getName(), item.getHour(), item.getDate()));
        }
        ReminderListAdapter2 adapter = new ReminderListAdapter2(SearchActivity.this,android.R.layout.simple_list_item_1,lstFound);
        listView.setAdapter(adapter);
    } else {
        ReminderListAdapter2 adapter = new ReminderListAdapter2(SearchActivity.this,android.R.layout.simple_list_item_1,ReminderList);
        listView.setAdapter(adapter);
    }
    return true;
}});


public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu_item,menu);
    MenuItem item = menu.findItem(R.id.action_search);
    searchView.setMenuItem(item);
    return true;
}



@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

}


@Override
public void OnItemClick(View view, int position, int id, String name, String date, String hour) {

}}

适配器的代码:

public class ReminderListAdapter2 extends ArrayAdapter<Reminder> {
private Context mContext;
private int mResource;
OnItemClickListener listener;

public ReminderListAdapter2(@NonNull Context context, int resource, @NonNull ArrayList<Reminder> objects) {
    super(context, resource, objects);
    mContext = context;
    mResource = resource;

}

public void setListener(OnItemClickListener listener) {
    this.listener = listener;
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    final int id = getItem(position).getId();
    final String name = getItem(position).getName();
    final String hour = getItem(position).getHour();
    final String date = getItem(position).getDate();


    LayoutInflater inflater = LayoutInflater.from(mContext);
    convertView = inflater.inflate(mResource, parent, false);
    final TextView resId = convertView.findViewById(R.id.textId);
    final TextView resName = convertView.findViewById(R.id.textName);
    final TextView resHour = convertView.findViewById(R.id.textHour);
    final TextView resDate = convertView.findViewById(R.id.textDate);

    resId.setText(String.valueOf(id));
    resName.setText(name);
    resHour.setText(hour);
    resDate.setText(date);

    return convertView;
}}

为什么对象引用为null?抱歉,这是一个愚蠢的错误,它是第一个应用程序。

我尝试将lstFound.add(new Reminder(item.getId(), item.getName(), item.getHour(), item.getDate()));更改为lstFound.add(this);,但这也是空引用。

2 个答案:

答案 0 :(得分:0)

关闭。

错误出在ReminderListAdapter2 adapter = new ReminderListAdapter2(SearchActivity.this, android.R.layout.simple_list_item_1,ReminderList);

androidR.layout.simple_list_item_1需要更改为R.layout.reminderview2

答案 1 :(得分:0)

According to documentation

  

要为数据集中的每个项目显示更多自定义视图,请实现ListAdapter。例如,扩展BaseAdapter并为

中的每个数据项创建并配置视图

如果是这样,则布局充气方法的第一个参数应为layout而不是int

例如convertView = getLayoutInflater().inflate(R.layout.list_item, container, false);

在您的情况下,您尝试使int膨胀而不是布局

convertView = inflater.inflate(mResource, parent, false);