在不同的活动SQLite中搜索数据

时间:2019-07-16 18:06:04

标签: android nullpointerexception android-sqlite

我正在尝试从Android Studio中的其他活动搜索数据。 我已经添加了代码和错误。请让我知道我哪里出了问题。

我在活动1中介绍了sqlite数据库。 我正在尝试使用searchView在活动2中进行查询。

公共类CancelledViewer扩展了AppCompatActivity {

SearchView searchView;
ListView listView;
ArrayAdapter<String> arrayAdapter;
ArrayList<String> names;
Cursor c;
int nameIndex;



@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(getApplicationContext(), OrderBook.class);
    startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cancelled_viewer);

    searchView = (SearchView) findViewById(R.id.searchView);
    listView = (ListView) findViewById(R.id.listView);
    names = new ArrayList<String>();
    arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, names);


    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {

            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {

            try {
                if (s.length() == 0) {
                    arrayAdapter.clear();

                } else {

                    c = PartyInfo.myDatabase.rawQuery("SELECT * FROM partyinfo WHERE name LIKE  '%" + s + "%'", null);
                    nameIndex = c.getColumnIndex("name");
                    arrayAdapter.clear();
                    c.moveToFirst();
                    while (c != null) {
                        names.add(c.getString(nameIndex).toUpperCase());
                        c.moveToNext();
                    }
                }
            } catch (Exception e) {
                Log.i("Error in code", e.toString());
            }

            return false;
        }
    });


}
}

代码错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference

0 个答案:

没有答案