Calling subclass method from other class - Android (java)

I have a working trivia game and am now trying to implement a highscores. I created a subclass DatabaseHelper class extending SQLiteOpenHelper. I am trying to call a method in the subclass DatabaseHelper from another class and am getting errors. The classes won't compile so there is no LogCat output. I have commented in the code the errors.

Code structure advice is appreciated as well!

Highscores.java

public class Highscores extends Activity {

    DatabaseHelper dh;
    SQLiteDatabase db;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        dh = new DatabaseHelper(context);
    }

    public class DatabaseHelper extends SQLiteOpenHelper { 

        public DatabaseHelper(Context context) { 
            //code
        }

        public boolean check(long score, int percentage) {
            //code
        }
    }
}

Results.java

public class Results extends Activity {

    DatabaseHelper dh;  //Error:  "DatabaseHelper cannot be resolved to a type."

    public void onCreate(Bundle savedInstanceState) {

        dh = new DatabaseHelper();  //Error:  "DatabaseHelper cannot be resolved to a type."
    }

    public void showResults() {

        if(dh.check(score, percentage) == true) {  //Error:  "DatabaseHelper cannot be resolved to a type."
      dh.insert(score, percentage);  //Error:  "DatabaseHelper cannot be resolved to a type."
        }
    }
}

Answers


Sounds like you are missing an import at the top of your Results.java

It should look something like import com.somepackage.DatabaseHelper;

Errors like this are typically either a typo in the class name or a missing import.


When you need a class outside of the class it is defined, I would suggest create that class as separate class. Otherwise you need to create instance of outer class to access inner class which would be creating instance of Activity and call DatabaseHelper on Activity instance, which is not a good approach in Android.

If you still would like to continue with existing approach, this is how you need to access it:

OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Please read nested classes tutorial


Need Your Help

Group By with Ordering

sql-server group-by sql-order-by

im looking for a little help with an SQL query, I've been racking my brains for a few hours.

Is there a workaround in VBA for sql queries with no results?

sql vba excel-vba dao adodb

I have a VBA program in which part of it queries our SQL servers and returns specific data. The problem is that the data isn't necessarily always there, which is expected. I need to query the dat...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.