Accessing Application class from Activity crashes - android

I am relatively new to Android programming. So please, forgive me for writing or asking anything stupid. I am working on an Android application in which I am using SQLite database to store data. I need to access this database from more Activities and after some research I have concluded that the best way to do so is using an Application class. But this is where my program stops working.
I have my Application class:
import android.app.Application;
public class MyApplication extends Application {
public CONTACT_DB = new CONTACT_DB(this);
}
where CONTACT_DB class is where I have defined my database and has this constructor:
private final Context context;
private DatabaseHelper DBHelper;
public CONTACT_DB(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
Now in my Activity, where I need to access this database
public class Add_contact extends Activity {
CONTACT_DB db;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_contact_layout);
MyApplication appState = ((MyApplication)getApplicationContext());
db = appState.db;
}
}
My program works fine, but when it gets to line
MyApplication appState = ((MyApplication)getApplicationContext());
then it crashes. I tried to use just a simple int instead of my CONTACT_DB class and it still srashes. Any ideas?

Have you declared your custom application class in the manifest?
For example:
<application
android:name="com.yournamespace.appname.MyApplication"
android:icon="#drawable/icon"
android:label="#string/app_name">

Related

Ormlite inside Intentservice - ClassCastException

I am trying to use Ormlite for Database handling from an intentservice. I have read about using an Application as subclass. I have tried the following
The application class:
public class MyApplication extends Application {
private volatile DatabaseHelper databaseHelper = null;
#Override
public void onCreate() {
super.onCreate();
}
public DatabaseHelper getHelper() {
if (databaseHelper == null) {
databaseHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
}
return databaseHelper;
}
}
In the intent service I defined:
private DatabaseHelper databaseHelper;
And in the onHandleIntent() method of the intentservice I have tried:
databaseHelper = ((MyApplication) getApplicationContext()).getHelper();
However I am getting ClassCastException, from Application to MyApplication
Can someone explain to me what I'm doing wrong?
Found the problem. I was declaring the application in the manifest as a different one from my main application.
What I needed is to modify the main application in AndroidManifest.xml. I pointed the main application to the custom application (MyApplication) that I created
Add this:
<Application android:name=".MyApplication">

Difference between my application object and application context object

I am looking for information regarding writing my own application class. In many tutorials on the net I have seen the following code:
class myapp extends Application
{
private static myapp mm;
private Context context;
public Context getContext()
{
return getApplicationContext();
}
public myapp getmyapp()
{
if(mm == null)
mm = new myapp();
return mm;
}
}
What is the difference in getting object of myapp and getApplicationContext and where to use object of myapp and where to use context object. I just want to clear the concept of usage of these objects.
that code is completely wrong:
public myapp getmyapp()
{
if(mm==null)
mm=new myapp();
return mm;
}
only the Android framework can instantiate the Application object. I mean, you can call new but the object won't be "connected" to the underlying framework
To have a static reference of the application object you should do as follows:
class MyApp extends Application{
// I fixed the names to follow some Java convention
private static MyApp instance;
#Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static MyApp getMyApp(){
return instance;
}
Regarding the context, the code is not wrong, but simply doesn't make any sense. That's because the Application object already is the application context. So there's not need to ask for it.
Context is what gives Android apps access to resources, file system specific folders, permissions, etc (what I said about the Android framework creates it). The Application is one class that extends Context, other examples are Activity and Service classes.
I hope it's a bit clearer.

How to get a Global variable from one class to another?

Hi have one database and I've created a Database class that has a private static class DbHelper extends SQLiteOpenHelper to help me manage my database.
This database is access from four different activities.
Now What I need is? I have a GlobalClass like this:
public class Question extends Application{
private String check;
public String getCheck() {
return check;
}
public void setCheck(String check) {
this.check = check;
}
}
In FirstScreen Activity I have a value for String check. If I get in other Activity Class its fine, no problem.
If I get in DBHelper I can't. I have tried like this:
final Question quiz = (Question) getApplicationContext();
final String check = quiz.getCheck();
it shows error in getApplicationContext(). How can I get that value in DBHelper class
Please let me know what is wrong with the syntax.
in DBHelper you will not have ApplicationContext till you don't pass it.
instead of this do one thing make a static String in your Application class and use it.
as Application class is a single ton instance which remains in memory till the end so it will not use much memory.
public class Question extends Application{
public static String check = "";
}
Retrieval of the value
public class DBHelper {
public method() {
String check = Question.check;
}
}
You don't have a Context in your DBHelper class.
You either need to pass in a Context when you instantiate the DBHelper class (preferred but not always practical) or get the Context from your Question by doing something like the following.
((Question)Question.getAppContext())
The Context is then returned via a getAppContext() method in Question.

Android: How to create application class object in BaseAdapter class

I am developing an Android application and maintaining Global Variables in an Application Class MyAppData. Now in order to use those global variables, I am creating the MyAppData object in my Activity as follows:
MyAppData mad;
mad = (MyAppData)getApplication();
As my activity have a custom Listview, I am using BaseAdapter to populate the LstView. Now i need to use the global variables in my BaseAdapter class. The following code doesnt allowing me to create an object of MyAppData class :
public class AlbumList_Adapter extends BaseAdapter{
Context context;
MyAppData mad;
public AlbumList_Adapter(Context context){
this.context = context
mad = (MyAppData)getApplication();
}
}
Even I had tried mad = (MyAppData)context; but no Luck. I dont know where I have mistaken.
You can have:
class MyApplication extends Application {
private static MyApplication mInstance;
#Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static MyApplication getInstance() { return mInstance; }
}
Then you can use MyApplication.getInstance() throughout your code.

Android: extending android.app.Application

I have a DBHandler class that will be used in several activities to do some crud operations. created a MyApp class (extends Application) to hold one instantiation of the DBHandler.
My DBHandler class needs a Context to create the SQLiteOpenHelper to populate the db, etc.
That's where the problem starts: in my MyApp constructor, I want to instantiate my DBHandler, so I wrote this:
public MyApp() {
super();
dbh = DBHandler(<WHAT DO I PASS HERE>);
}
I tried getApplicationContext(), getBaseContext(), 'this'... nothing seems to be a fully-instantiated context at this point. I get a NPE when the SQLiteOpenHelper tries ctx.getResources().
A workaround: create the DBHandler and set it in the onCreate of my main class. -> UGLY (call me a aesthetician)
Question: is there a way to do it when Android creates MyApp?
Thanks!
Creating your DBHandler in MyApp.onCreate() is the proper way to do what you want.
#Override
public void onCreate() {
super.onCreate();
dbh = new DBHandler(this);
}

Categories

Resources