简体   繁体   中英

Cannot resolve symbol message for method from a class

I created following method in SQLiteOpenHelper subclass:

public List<Data> getAll(){
        List<Data> returnList = new ArrayList<>();
        String queryString = "SELECT * FROM " + table;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(queryString, null);
        if(cursor.moveToFirst()){
            do {
                int ID = cursor.getInt(0);
                int soundamp = cursor.getInt(1);
                String date = cursor.getString(2);
                String time = cursor.getString(3);
                double lat = cursor.getDouble(4);
                double lon = cursor.getDouble(5);
                Data newData = new Data(ID, soundamp, date, time, lat, lon);
                returnList.add(newData);
            }while(cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return returnList;
    }

But when I try to use this method in MainActivity on button click like this:

else if(v==viewAll){
            SQLite sql = new SQLite(MainActivity.this);
            List<Data> all = new sql.getAll();
        }

"getAll" gets highlighted red and I get the following message when I hover over it: Cannot resolve symbol 'getAll'. Now I have tried Invalidating caches and restarting project but it didn't work, I have also tried Importing to a new project but that didn't work either.

What if you remove the new keyword when you call getAll() ? You are not creating a new object there, your all list is just getting back the list your return from the method.

else if(v==viewAll){
            SQLite sql = new SQLite(MainActivity.this);
            List<Data> all = new sql.getAll(); //remove new here
        }

Creating a getAll() method in a SQLiteOpenHelper class doesn't help if you want to call getAll() on an instance of the SQLite class.

Calling getAll() on an instance of the SQLite class is only valid if SQLite or one of its superclasses contains such a method (and most probably, SQLiteOpenHelper is not a superclass of SQLite ).

EDIT:

I overlooked the new keyword that @net00 spotted, and of course that makes the attempted call unintellegible to the compiler. new must be followed by a classname, not a method call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM