我不知道为什么会这样。当我检查DDMS时,也没有文件目录。我正在尝试在我的Application子类中访问此文件夹。知道为什么会这样吗? 我需要应用程序上下文是全局的,所以我可以使用不扩展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();
}
}
我的清单文件:
<?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>
答案 0 :(得分:7)
我认为问题是你正在尝试在构造函数中进行初始化。将构造函数中的代码移动到Application类的OnCreate。
@Override
public void onCreate() {
super.onCreate();
if(instance != null)
instance = MultiPraticApp.this;
if(helper != null)
helper = new MultiPraticAppHelper();
}