0

我正在尝试从服务中获取我的应用程序的资产,但我得到了 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 是从 Service 调用的。我有上下文。我读过我应该使用主要活动中的 getAssets 。请告诉我为什么,有什么区别。

4

1 回答 1

0

下次请在此处注明第 41 行。恕我直言,这只是使堆栈跟踪无法使用的部分代码

我认为的问题在这里:

AssetManager assetManager = getAssets();
            for (String file : assetManager.list("tessdata")) {
                InputStream in = assetManager.open("tessdata/" + file);

如果assetManager.list("tessdata") 返回一个null(没有tessdata),该nullString 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);
...
    }
}
于 2013-01-21T06:15:57.097 回答