![](/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.