我正在尝试从Service获取我的应用程序的资产,并且我得到nullpointexception。 我发现很少有关于它的文章。但我不知道我的错误。
代码在哪里:
public class OCRService extends IntentService{
private static final String TAG = "OCRService";
public static final String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/SimpleAndroidOCR/";
public OCRService() {
super("OCRService");
String path = new String(DATA_PATH + "tessdata/");
File dir = new File(path);
if (!new File(path).exists() && !dir.mkdirs()) {
Log.e(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
return;
} else {
Log.v(TAG, "Created directory " + path + " on sdcard");
if (!(new File(DATA_PATH + "tessdata/" + "eng.traineddata")).exists()) {
try {
AssetManager assetManager = getAssets();
for (String file : assetManager.list("tessdata")) {
InputStream in = assetManager.open("tessdata/" + file);
OutputStream out = new FileOutputStream(DATA_PATH + "tessdata/" + file);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
Log.v(TAG, "Copying ocr traineddata sacceded");
} catch (IOException e) {
Log.e(TAG, "Was unable to copy osr traineddata "+ e.toString());
}
}
}
}
例外情况在哪里:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate service com.pc.droidhelper.services.OCRService: java.lang.NullPointerException
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2241)
at android.app.ActivityThread.access$1600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
at com.pc.droidhelper.services.OCRService.<init>(OCRService.java:41)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2238)
... 10 more
您可以看到从服务中调用getAssets。我在那里有背景。我已经读过,我应该使用主要活动中的getAssets。请告诉我原因,有什么区别。
答案 0 :(得分:0)
下一次,请在此处注明第41行。恕我直言这个 只是部分代码使堆栈跟踪无法使用
我认为问题在于:
AssetManager assetManager = getAssets();
for (String file : assetManager.list("tessdata")) {
InputStream in = assetManager.open("tessdata/" + file);
如果assetManager.list(“tessdata”)返回null(没有tessdata
),则null作为一个条目进入String file
,然后你正在调用
InputStream in = assetManager.open("tessdata/" + file);
如果"tessdata/" + file
不存在, "tessdata"
将不存在
所以,解决方案可能是:
for (String file : assetManager.list("tessdata")) {
if(file!=null){
InputStream in = assetManager.open("tessdata/" + file);
...
}
}