3

First up, I am new to android apps and am not working solo on this. My team mate has taken design while I handle this, and asked me to set up the database and the method to do this, etc etc.

So while most of this seems to be ok, I put:

Context context = null;
DataBaseHelper drinksDataBase = new DataBaseHelper(context); 

into his main activity.

The constructer is as follows:

public DataBaseHelper(Context  context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
    try{
        createDataBase();
    }
    catch(IOException e){

    }
}

Ignoring the null, which I am assuming is the current cause of the app crashing, how would I go about getting the proper context for the app so that I can make my database work?

It actually seems to be crashing on this.getReadableDatabase() so whether that is the null context or not, I don't know.

Logcat is failing to launch due to:

[2012-10-12 10:37:57 - Unexpected error while launching logcat. Try reselecting the device.] device not found
com.android.ddmlib.AdbCommandRejectedException: device not found
    at com.android.ddmlib.AdbHelper.setDevice(AdbHelper.java:752)
    at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:373)
    at com.android.ddmlib.Device.executeShellCommand(Device.java:462)
    at com.android.ddmuilib.logcat.LogCatReceiver$1.run(LogCatReceiver.java:109)
    at java.lang.Thread.run(Unknown Source)  

Thanks in advance,

James

Seraphim's
  • 12,559
  • 20
  • 88
  • 129
James Sunderland
  • 135
  • 1
  • 1
  • 13

2 Answers2

15

Here is what I usually do:

 public class MyApplication extends Application {
    private static MyApplication  instance;
    public MyApplication()
    {
       instance = this;
    }
    public static Context getContext()
    {
       return instance;
    } 

And just set that class into the manifest

 <application
        android:name="my.workspace.MyApplication"
...
 >

After that just call MyApplication.getContext(), where you need it.

Aleksandar Stojadinovic
  • 4,851
  • 1
  • 34
  • 56
3

If you're using eclipse it's beyond child's play. Just make a field somewhere "private Context context" then you go to generate constructor from fields in the source tab. Just spits it out for you. Then when you need to make an instance of the class. Usually "this" in parameters will suffice

EDIT


I hope this helps, I'd rather not prolong a discussion here. in your database:

public DataBaseHelper(Context  context) {
    super(context, DB_NAME, null, 1);
}

public Cursor getDrinks() {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(DB_NAME, null, null, null, null, null, null);
        return cursor;
    }

Then in your activity (in oncreate):

private Cursor c;

...
DataBaseHelper drinksDataBase = new DataBaseHelper(this);
c = drinksDataBase.getDrinks(); 
c.moveToFirst();
mango
  • 5,577
  • 4
  • 29
  • 41
  • The Context context field is inside the DataBaseHelper class, I just need it outside that class, so that I can put it into this class. Would your suggestion work for that? – James Sunderland Oct 11 '12 at 23:21
  • hmm so that did give me a context, thank you, but it still crashes on getReadableDatabase(), so I guess it wasnt the null value causing the crash, yes? – James Sunderland Oct 11 '12 at 23:30
  • I'm sorry i guess i didn't get question perfectly. If the context is set up in your database class constructor well then all you need is to put it in your parameters correctly? if you're instantiating in oncreate - then DataBaseHelper drinksDataBase = new DataBaseHelper(**this**); is all you need. but if you're in some inner event handler, then you might probably need DataBaseHelper drinksDataBase = new DataBaseHelper(**ClassName.this**); "Context context = null;" does nothing for this specific procedure and you shouldn't leave it as null anyway. – mango Oct 11 '12 at 23:31
  • if you have a crash, you should post the logcat or some code at least, i have no idea of knowing what's going on. that null value won't cause a crash unless it's being used. but that should never be there anyway. – mango Oct 11 '12 at 23:36
  • I apologize for being a pain with this but I really am at a rather basic understanding with android, and its been about a year or so since I've done java. I am going to put the constructer in my first post, could you tell me if it would work with your idea of just using this? – James Sunderland Oct 11 '12 at 23:37
  • public DataBaseHelper(Context context) { super(context, DB_NAME, null, 1); } – mango Oct 11 '12 at 23:44
  • Why do you want me to remove this.context = context and the createDatabase call? just curious. – James Sunderland Oct 11 '12 at 23:46
  • the this.context is unnecessary, really. And the createDatabase shouldn't be done in the constructor, contructors are for parameters. how are you trying to do the getReadable database? – mango Oct 11 '12 at 23:49
  • createDatabase checks for if the database currently exists, and if it doesnt, calls this.getReadableDatabase(); which, seeing as I dont have a function called that, I assume it is getting it from SQLiteOpenHelper? – James Sunderland Oct 11 '12 at 23:53
  • you can check if the database exists in the onCreate method of the database, that's where you should. i suspect something is amiss there. try testing that code that i just posted. I'm sorry if i couldn't help out but i just haven't seen enough: http://www.vogella.com/articles/AndroidSQLite/article.html <-- that is a good resource, try that. – mango Oct 12 '12 at 00:01
  • don't apologize, I am just glad someone is willing to help. The internet is rather unhelpful a lot of the time. – James Sunderland Oct 12 '12 at 00:08
  • And stupid me, I just noticed your code and modified mine to use that, but unfortunately it still stops unexpectedly, so off I go to try and find a solution. Thanks for the help and the code. – James Sunderland Oct 12 '12 at 00:30
  • Ok so I found the problem, its when I try to use the drinksDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); command in my database's onCreate method. So apparently it cant find the database? – James Sunderland Oct 12 '12 at 00:36
  • This really isn't the place to do these long discussions. The only commands you need to get a readable database is right there. If that didn't work in its simplest form, then it's not being created properly. I suggest you scan through after a tutorial or 2 – mango Oct 12 '12 at 03:53
  • http://developer.android.com/training/notepad/index.html here work through the source for this. It should show you the way – mango Oct 12 '12 at 04:04
  • Yeah I got it all working, now im wokring on how to use the curser and such, but my work is progressing well. Thanks for all the help – James Sunderland Oct 12 '12 at 06:18