标签: java android
private Context context; public MainActivity(Context con) { super(); this.context = con; } private InputStream is; public MainActivity() { /// NPE HERE this.is= MainActivity.this.context.getResources().openRawResource(R.raw.allstops); } public InputStream getIs() { return is; }
P.S:我只是在onCreate()方法之后才做到这一点。 allstops是res / raw文件夹中的xml文件。
答案 0 :(得分:2)
默认构造函数(您设置this.is的位置)在设置this.context之前完成。导致context的{{1}}将null。您需要将NullPointerException行移至this.is作业之后。
this.is
this.context
context
null
NullPointerException