2013-07-15 6 views
0

현재 Eclipse 및 JUnit을 통해 Android 용 테스트 프레임 워크를 만드는 중입니다. 마지막으로 구현하는 것은 구성 파일과 판독기로 구성 파일을 사용하여 필요할 때 다양한 속성을 변경할 수 있습니다.Eclipse Android 구성 파일은 항상 null을 반환합니다.

MainFramework (project) 
    Base package 
    Utility package 
    Config class 

Testing (project) 
    examples package 
    Artist testing class 

설정은 환경 설정 클래스는 다음과 같다 :

공용 클래스 구성 {

private static Config instance; 
public Context context; 
public static Properties prop; 

public static StorefrontConfig getInstance(Context context) { 

    if(instance == null) 
     instance = new StorefrontConfig(context); 
    return instance; 
} 

protected StorefrontConfig(Context cont) { 
    context = cont; 
    AssetManager manager = context.getAssets(); 
    try { 
     InputStream instream = manager.open("config"); 
     readConfig(instream); 
    } 
    catch (Exception e) { 
     Log.d("cool", "Failed to create properly initialized config class"); 
    } 
} 

private static void readConfig(InputStream instream) { 

    try { 
     String line = ""; 
     BufferedReader read = new BufferedReader(new InputStreamReader(instream)); 

     while ((line = read.readLine()) != null) { 
      String[] split_line = line.split("=", 2); 
      prop.setProperty(split_line[0], split_line[1]); 
     } 

     prop.store(new FileOutputStream("config.properties"), "Default and local config files"); 
     read.close(); 
    } 
    catch (Exception e) { 
     Log.d("cool", "Failed to create properly initialized config class"); 
    } 
} 

public String getProperty (String propertyKey) { 

    try { 
     return prop.getProperty(propertyKey); 
    } 
    catch (Exception e) { 
     Log.d("cool", "Failed to access property"); 
     return null; 
    } 
} 

public Context getContext() { 
    return context; 
} 

을 내 코드에서 getProperty에() 메서드를 호출 할 때, 다음과 같이 내 프레임 워크의 구조는 항상 null을 반환합니다. 그러나 처음에 값을 읽거나 쓰지 못하거나 무슨 일이 일어나고 있는지 전혀 알 수 없습니다. 내가 아는 모두는 내 프로그램이 하드 코딩 된 값으로 작동하지만이 클래스를 사용하고 필요할 때 코드에서 config.getProperty()를 통해 참조 할 때가 아닙니다. (내 메인 프레임 워크에는 모든 테스트에서 상속 된 Config 클래스가 있습니다.)

도움이 될 것입니다. 내가 생각할 수있는 유일한 점은 Java의 Properties 클래스를 Android에서 사용할 수 없다는 것입니다.

답변

0

자바의 속성은 Android에서 사용할 수 있습니다.

prop을 인스턴스화하지 않은 것처럼 보입니다. prop.getProperty(propertyKey)으로 전화하면 NullPointerException이됩니다. 어떤 시점에서 propprop = new Properties();으로 인스턴스화해야합니다. these examples을 확인하십시오. 숫자 2는 아마도 readConfig()에서와 같이 속성 파일을 수동으로 구문 분석 할 필요가 없으므로 삶을 편하게 만듭니다.

관련 문제