![](/img/trans.png)
[英]No project.properties file and NullPointerException during build of new android application
[英]NullPointerException when using properties file in android application
我想使用屬性文件來讀取我的Android應用程序中的一些配置數據。 當我將屬性文件放置在處理程序的class文件夾中時,一切正常,但是我想將屬性文件放置在項目根目錄中。 我已經用相對路徑( ../../../
等)嘗試過,但是沒有用。
有沒有一種“簡便”的方法來做到這一點( /config.properties
也不起作用)?
這是我的代碼:
private Properties prop;
public PropertiesHandler() {
this.prop = new Properties();
InputStream input = null;
try {
input = getClass().getResourceAsStream("config.properties");
//input = new FileInputStream("config.properties");
prop.load(input);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
任何幫助將不勝感激。
編輯:
不幸的是,在應用建議的解決方案時出現了此異常:
10-08 15:10:05.903: E/AndroidRuntime(967): FATAL EXCEPTION: main
10-08 15:10:05.903: E/AndroidRuntime(967): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dev.app1234/com.dev.app1234.Register}: java.lang.NullPointerException
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.os.Looper.loop(Looper.java:137)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-08 15:10:05.903: E/AndroidRuntime(967): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 15:10:05.903: E/AndroidRuntime(967): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 15:10:05.903: E/AndroidRuntime(967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-08 15:10:05.903: E/AndroidRuntime(967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 15:10:05.903: E/AndroidRuntime(967): at dalvik.system.NativeStart.main(Native Method)
10-08 15:10:05.903: E/AndroidRuntime(967): Caused by: java.lang.NullPointerException
10-08 15:10:05.903: E/AndroidRuntime(967): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
10-08 15:10:05.903: E/AndroidRuntime(967): at com.dev.app1234.Register.<init>(Register.java:68)
10-08 15:10:05.903: E/AndroidRuntime(967): at java.lang.Class.newInstanceImpl(Native Method)
10-08 15:10:05.903: E/AndroidRuntime(967): at java.lang.Class.newInstance(Class.java:1319)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-08 15:10:05.903: E/AndroidRuntime(967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-08 15:10:05.903: E/AndroidRuntime(967): ... 11 more
這是我的代碼:
public PropertiesHandler(Context context) {
this.prop = new Properties();
InputStream input = null;
try {
input = context.getAssets().open("config.properties");
我這樣稱呼它:
private final String URL = new PropertiesHandler(this.getApplicationContext()).getUrl()
+ "url.php";
編輯2:
碼:
private final String URL = new PropertiesHandler(Register.this).getUrl()
+ "url.php";
堆棧跟蹤:
10-08 20:45:14.421: E/AndroidRuntime(1024): FATAL EXCEPTION: main
10-08 20:45:14.421: E/AndroidRuntime(1024): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dev.app1234/com.dev.app1234.Register}: java.lang.NullPointerException
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.os.Looper.loop(Looper.java:137)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-08 20:45:14.421: E/AndroidRuntime(1024): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 20:45:14.421: E/AndroidRuntime(1024): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 20:45:14.421: E/AndroidRuntime(1024): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-08 20:45:14.421: E/AndroidRuntime(1024): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 20:45:14.421: E/AndroidRuntime(1024): at dalvik.system.NativeStart.main(Native Method)
10-08 20:45:14.421: E/AndroidRuntime(1024): Caused by: java.lang.NullPointerException
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.content.ContextWrapper.getAssets(ContextWrapper.java:75)
10-08 20:45:14.421: E/AndroidRuntime(1024): at com.dev.app1234.model.PropertiesHandler.<init>(PropertiesHandler.java:25)
10-08 20:45:14.421: E/AndroidRuntime(1024): at com.dev.app1234.Register.<init>(Register.java:68)
10-08 20:45:14.421: E/AndroidRuntime(1024): at java.lang.Class.newInstanceImpl(Native Method)
10-08 20:45:14.421: E/AndroidRuntime(1024): at java.lang.Class.newInstance(Class.java:1319)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-08 20:45:14.421: E/AndroidRuntime(1024): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-08 20:45:14.421: E/AndroidRuntime(1024): ... 11 more
如果我是你,我會將配置文件放在資產文件夾中,並像這樣獲得InputStream:
InputStream is = context.getAssets().open("config.properties");
例如,上下文是您的活動。
這個電話:
private final String URL = new PropertiesHandler(Register.this).getUrl() + "url.php";
需要放置在活動的onCreate()
方法中。 像這樣:
class Register extends Activity {
private String URL;
// ...
protected void onCreate(Bundle savedInstanceState) {
// ...
URL = new PropertiesHandler(Register.this).getUrl() + "url.php";
// ...
}
}
通過在創建活動之前創建自定義類,可以有效地在以下代碼中傳遞null
: new PropertiesHandler(Register.this)
,因此在堆棧跟蹤中出現的null指針錯誤。
之所以會發生這種錯誤,通常是因為Android中如此多的有用系統調用都需要Context
。 發生這種情況的一個很好的線索是在堆棧跟蹤中看到對<init>
虛擬方法的引用,該方法引用了類中所有未包含在其他方法中的變量初始化器。 就像給出的堆棧跟蹤中的這一行:
10-08 20:45:14.421: E/AndroidRuntime(1024): at com.dev.app1234.Register.<init>(Register.java:68)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.