Monday 15 March 2010

java - Cannot use 'this' in static context -


I have done a fair amount of coding in C # but I'm completely new to Java I have an Android application I am trying to learn which interacts with SQLite.

My purpose is a class, which contains all functions, which clean and elaborate database execution - which can also be said with a line of code, for example;

I have a DBAdapter class that I have read in the tutorial:

  public category DBAdapter {static last string KEY_ROWID = "_id"; Fixed final string KEY_NAME = "name"; Fixed final string KEY_EMAIL = "email"; Fixed final string TAG = "DBAdapter"; Fixed final string DATABASE_NAME = "MyDB"; Fixed final string DATABASE_TABLE = "Contact"; Fixed final difference DATABASE_VERSION = 1; Fixed final string DATABASE_CREATE = "Create a table contact (_id integer primary key autoincrement," + "name text is not redundant, email text is not null);"; Last Reference Reference; DatabaseHelperDBHLper; SQLiteDatabase db; Public DBAdapter (reference CTX) {this.context = ctx; = New database support (reference) on DBechChart; } Private Static Class DatabaseHelper SQLiteOpenHelper (reference reference) {super (context, DATABASE_NAME, blank, databasebaseinsins) is extensively; } @ Override Public Wide On Crack (SQLiteDatabase db) {try {db.execSQL (DATABASE_CREATE); } Hold (SQLException pre) {ex.printStackTrace (); }}} // --- opens the database --- opens the public DBAdapter (Open) SQLException {db = DBHelper.getWritableDatabase (); This return; } // --- closes the database --- the public is closed () {DBHelper.close (); } // Some other database functions are here ... Inserts, updates, etc.}   

and I have created my call to handle all calls of DBadaptator:

  public Static class database enhances activity activity {Fixed DBAdetter DB; // It is said that the activity has been created for the first time. Override public null on the creation (bundle saved instenstate) {super.onCreate (SavedInstanceState); SetContentView (R.layout.activity_main); } Public static long connectivity (string name, string email) {if (db == null) {db = new DBAdapter (this); // & lt; --- compiler error here} db.open (); Long ID = DB.Instant Contact ("Joe Blogs", "Jodi @ Blogs."); Db.close (); Return id; }}   

In the addContact method in this line: db = new DBAdapter (this); , I get the following error: can not use 'this' in a constant reference

I am familiar with the OOP concepts, so I think Why I'm getting this error - but being new to Java, I'm looking for alternative methods I'm trying to get DBadaptor moves the class constructor into the context parameter, but I am unsure why I have not written that class myself.

To clarify: I understand why the error is going to happen. The DBAdapter class creator takes a reference parameter, and I do not know when I'm using it steadily, then I want to refer to it. I want the class to be stable, because I want to use it every time.

I think my real question would be "Why is SQLiteOpenHelper a necessity?"

You defined a static method here:

  Insert public static long time (string name, string email) < / Code>  

Static Management and Class Variables class and you can not use the this keyword in the specific example of any class Because it reflects the current example, the method must be declared as a manifesto method, method method to remove static from the method method, if in fact the method argument is at the current frequency status depends on.

I believe that the problem with using the static method inside this will be that if your code stays in the static method < Em> ClassName says StaticMethodName () , it will not know how to solve this in this order.

No comments:

Post a Comment