Android的getAssets()方法将抛出NullPointerException异常 [英] Android getAssets() method throws NullPointerException

查看:2753
本文介绍了Android的getAssets()方法将抛出NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从服务我的应用程序的资产,我得到nullpointexception。
我发现关于它。但是几篇文章我不明白的地方我的错误。

在哪儿code:

 公共类OCRService扩展IntentService {私有静态最后弦乐TAG =OCRService;
公共静态最后弦乐DATA_PATH = Environment.getExternalStorageDirectory()的toString()+/ SimpleAndroidOCR /;
公共OCRService(){
    超级(OCRService);
    字符串路径=新的String(DATA_PATH +tessdata /);
    文件DIR =新的文件(路径);
    如果(新文件(路径).exists()及!&安培;!dir.mkdirs()){
        Log.e(TAG,错误:目录的创建+路径+关于SD卡失败);
        返回;
    }其他{
        Log.v(TAG,创建目录+SD卡上的路径+);
        如果(!(新文件(DATA_PATH +tessdata /+eng.traineddata))。存在()){
            尝试{                AssetManager assetManager = getAssets();
                对于(字符串的文件:assetManager.list(tessdata)){
                    InputStream的时间= assetManager.open(tessdata /+文件);
                    出的OutputStream =新的FileOutputStream(DATA_PATH +tessdata /+文件);
                    字节[] buf中=新的字节[1024];
                    INT LEN;
                    而((LEN = in.read(BUF))大于0){
                        out.write(BUF,0,LEN);
                    }
                    附寄();
                    out.close();
                }
                Log.v(TAG,复制OCR traineddata sacceded);
            }赶上(IOException异常五){
                Log.e(TAG,是无法复制OSR traineddata+ e.toString());
            }
        }
    }
}

在哪里是个例外:

 致命异常:主要
 了java.lang.RuntimeException:无法实例化服务com.pc.droidhelper.services.OCRService:显示java.lang.NullPointerException
    在android.app.ActivityThread.handleCreateService(ActivityThread.java:2241)
    在android.app.ActivityThread.access $ 1600(ActivityThread.java:127)
    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1205)
    在android.os.Handler.dispatchMessage(Handler.java:99)
    在android.os.Looper.loop(Looper.java:137)
    在android.app.ActivityThread.main(ActivityThread.java:4476)
    在java.lang.reflect.Method.invokeNative(本机方法)
    在java.lang.reflect.Method.invoke(Method.java:511)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:816)
    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
    在dalvik.system.NativeStart.main(本机方法)
 显示java.lang.NullPointerException:产生的原因
    在android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
    在com.pc.droidhelper.services.OCRService<&初始化GT;(OCRService.java:41)
    在java.lang.Class.newInstanceImpl(本机方法)
    在java.lang.Class.newInstance(Class.java:1319)
    在android.app.ActivityThread.handleCreateService(ActivityThread.java:2238)
    ... 10个

你可以看到getAssets从服务调用。和我有背景有。我已阅读,我chould使用getAssets从主要活动。请电话我为什么,有什么区别。


解决方案

  

下一次,请注明这是行号41在这里。恕我直言,这
  只是局部的code使得堆栈跟踪不可用


我认为这个问题是在这里:

  AssetManager assetManager = getAssets();
            对于(字符串的文件:assetManager.list(tessdata)){
                InputStream的时间= assetManager.open(tessdata /+文件);

如果assetManager.list(tessdata)返回一个空(没有 tessdata ),即空进入字符串文件为一个条目,然后你调用

 中的InputStream = assetManager.open(tessdata /+文件);

tessdata /+文件将不会存在,如果tessdata不存在

因此​​,该解决方案可能是:

解决方案

 为(字符串的文件:assetManager.list(tessdata)){
如果(文件!= NULL){
                InputStream的时间= assetManager.open(tessdata /+文件);
...
    }
}

I'm trying to get assets of my app from Service and i get nullpointexception. I found few articles about it.but i don't get where my mistake.

Where is the code:

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());
            }
        }
    }
}

Where is the exception:

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

as you can see getAssets is called from Service. and i have context there. I have read that I chould use getAssets from main activity. please tel my why, what is the difference.

解决方案

The next time, please indicate which is line number 41 here. IMHO this was only partial code making the stack trace unusable

The problem i think is here:

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

if assetManager.list("tessdata") returns a null (that there is no tessdata), that null goes into String file as one entry, then you're calling

InputStream in = assetManager.open("tessdata/" + file);

"tessdata/" + file will not exist if "tessdata" didn't exist

So, the solution might be:

Solution

for (String file : assetManager.list("tessdata")) {
if(file!=null){
                InputStream in = assetManager.open("tessdata/" + file);
...
    }
}

这篇关于Android的getAssets()方法将抛出NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆