Android: Signed APK issue - no default constructor when there is one - android

I'm getting the
'This class should provide a default constructor'
error when i'm trying to build the APK
this is my DBHelper class:
public class DBHelper extends SQLiteOpenHelper {
// create variables
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
// onCreate
I was under the impression that
public DBHelper(Context context)
was the default constructor? And have checked other answers with this and can't find anything to help...
Thanks in advance

A default constructor is a constructor without any arguments.
SQLiteOpenHelper needs at least a context so you won't be able to create a default constructor for your DBHelper. Are you sure this is the class causing this error ?

As the error suggest
Error: This class should provide a default constructor (a public constructor with no arguments)
try this way:
public class DBHelper extends SQLiteOpenHelper {
// create variables
public DBHelper(){
super();
}
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
// onCreate

Related

Android different databases for different users

I want to see if there is a way to assign different database names to different users that login to an app. I'm trying to write a small app that assigns a DB to a user based on their username/email.
I tried setting DATABASE_NAME that is passed into
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
to the users' email addresses but it doesn't work. It loads the same data for all users.
On logout, I close the database and clean up and on startup, everytime I start a new database connection with the name already properly assigned...
Can someone help me understand what I'm missing?
You can just pass the name of the database to your SQLiteOpenHelper class constructor (along with required Context instance) instead of hardcoding it as did DATABASE_NAME:
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context, String dbName, int dbVersion) {
super(context, dbName, null, dbVersion);
}
...
}

Are all databases provided by a SQLiteOpenHelper constructed with the same name within an app guaranteed to be identical?

If multiple activities within an app call the constructor of my SQLiteOpenHelper with themselves as the context argument, can I be sure that they will all access the same database?
For example, let's say I have:
package foo.bar;
public class Activity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLiteDatabase db = new MySQLiteOpenHelper(this).getReadableDatabase();
:
}
}
and
package foo.bar.baz;
public class Activity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLiteDatabase db = new MySQLiteOpenHelper(this).getReadableDatabase();
:
}
}
Here's in the skeleton of my SQLiteOpenHelper subclass:
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "comments.db";
private static final int DATABASE_VERSION = 1;
public MySQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
:
}
I can't find anything in the documentation that guarantees that the two activities get the same database. On the other hand, I haven't seen any mentions of people getting different databases from contexts within a single application. Are the database paths guaranteed to be identical?
SQLiteOpenHelper has 2 constructors and the second parameter for both of them is the database file name.
If you used the same database file name when using SQLiteOpenHelper from different activities, you will get access to the same database.
This is usually taken care of in the constructor of the inheriting class you create - DATABASE_NAME is a constant:
public MySQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
if you do it as the Android folks recommend (http://developer.android.com/guide/topics/data/data-storage.html#db)
Then yes, all activities within an app will see the same database

Get value of string.xml in android

my question is simple. How can i get access to the string value in res/values/string.xlm in a extends SQLiteOpenHelper because getString method is use with an Activity ?
u can get context object through constructor.
class DatabaseHelper extends SQLiteOpenHelper {
private Context mContext;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
}
Try
Receive Context in your SQLiteOpenHelper class and then use Context like
context.getResources().getString(R.string.yourStringNameFromStrings);
You need a reference to Context. The constructor of SQLiteOpenHelper accepts a context, you can save it in a local variable. If c is context variable, and myString is the name of string resource in strings.xml, then you can get string value as:
C.getString(R.string.myString);

Trouble with Constructor with helper

VPAdapter.java
public class VPAdapter extends PagerAdapter
{
public static String[] titles;
public final Context context;
public int[] scrollPosition;
JSONArray categories = null;
JSONArray newstype = null;
JSONObject json;
DatabaseHandler db = new DatabaseHandler(context)//error:The blank final field context may not have been initialized
...
}
DatabaseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
On VPAdapter.java I wanted to access DatabaseHandler anywhere, but there is problem with the constructor. What is the proper way I should write them?
You cannot pass a variable that was not initialize.
On your second line of the function you declare the context variable but you don't assign any value to it.
The last line should be written in the constructor of VPAdapter. The constructor should get a context variable. When you call your constructor you probably want to use the application context, but you might send also an activity (Activity inherit from context) but this is usually not recommended (But it really depends on your code)
Because your Context is null first initialize your context than you can pass that context to your database handler constructor.
Context context = getApplicationContext();
Or try below code
For example initialize your Context with your activity context.
Create constructor of your APAdapter class and call that constructor from your activity. Same way as you create for database handler.
public APAdapter(Context context) {
this.context = context;
}
than pass that context to your database handler.

What are the parameters for constructor of class cl_DBHandler which extends SQLiteOpenHelper in android?

i have created class cl_DBHandlerwhich extends SQLiteOpenHelper. and i have created class my_srvcwhich extends service. i have intialise object m_DBHandlerof class cl_DBHandlerin service but while initialization object i have to pass parameters to cl_DBHandler(context, name, factory, version), i am not getting exactly what to pass in this.
i have pass like this cl_DBHandler(getappcontext(),"databasename.sqlite",null,1)
2nd thing: i am calling one function from class with help of m_DBHandler object but i am getting value null of object m_DBHandler at every call though i have intialise that object in service.can anyone tell me or guide me to solve this problem.
Thanks in Advance--
Use the following constructor for your cl_DBHandler class
public static final String DATABASE_NAME = "MyDBName.db";
public static final int DATABASE_VERSION = 1;
public cl_DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
In your service or activity:
cl_DBHandler m_DBHandler = new cl_DBHandler(this);
Then open your database:
SQLiteDatabase db;
try {
db = m_DBHandler.getWritableDatabase();
} catch (SQLiteException ex) {
db = m_DBHandler.getReadableDatabase();
}
// do anything you like for example:
Cursor cursor = db.rawQuery(strQuery, null);

Categories

Resources