Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application context returning null when using getFilesDir()

I don't know why this is happening. When i check the DDMS there is no files dir too. I'm trying to access this folder at my Application subclass. Any idea why this is happening? I need the application context to be global so I can use on classes that doesn't extends Activity.

package mci.multipratic;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import android.app.Application;
import android.content.Context;

public class MultiPraticApp extends Application
{   
private static MultiPraticApp instance;
public static MultiPraticAppHelper helper;

public MultiPraticApp()
{       
    instance = MultiPraticApp.this;
    helper = new MultiPraticAppHelper();
}   

public static Context getContext()
{       
    return instance;
}
}

class MultiPraticAppHelper
{
private int offsets = 0;
private int productIndex = 0;
private int recipeId = 0;
private Recipe selectedRecipe;  
private Properties configFile;

public MultiPraticAppHelper()
{
    Context context = MultiPraticApp.getContext();                      

    String path = context.getFilesDir() + "/config.properties";

    File file = new File(path);

    try
    {
        FileInputStream fis = new FileInputStream(file);

        Properties properties = new Properties();
        properties.load(fis);

        fis.close();
    }catch(Exception ex)
    {
        ex.printStackTrace();
    }
}

My manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mci.multipratic"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="4" />  

<application android:name=".MultiPraticApp"        
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />                
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".EnableProductsActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">          
    </activity>

    <activity
        android:name=".RecipeSelectionActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">           
    </activity>

    <activity
        android:name=".ProductAdjustsActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
        >
    </activity>               
</application>  

like image 794
Comic Sans MS Lover Avatar asked Feb 09 '12 18:02

Comic Sans MS Lover


1 Answers

I believe the problem is you are trying to do initialization in the constructor. Move the code in constructor to OnCreate of Application class.

@Override
public void onCreate() {
  super.onCreate();
  if(instance != null)
     instance = MultiPraticApp.this;
  if(helper != null)
     helper = new MultiPraticAppHelper();

}
like image 93
nandeesh Avatar answered Nov 15 '22 07:11

nandeesh