从资产中检索图像时出错

时间:2014-06-28 16:28:00

标签: android bitmap assets decoder

我需要从assets文件夹中检索图像,我需要使用Bitmap来处理图像。该文件的名称是image.jpg。这是代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AssetManager manager = getAssets();

        try {
            InputStream is = manager.open("image.jpg");
            Bitmap img = BitmapFactory.decodeStream(is);
            ImageView imageView = (ImageView) this.findViewById(R.id.ImageView1);
                imageView.setImageBitmap(img);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

但我无法理解应用崩溃的原因。这是错误日志。

06-28 18:17:16.488: E/AndroidRuntime(10567): FATAL EXCEPTION: main
06-28 18:17:16.488: E/AndroidRuntime(10567): Process: com.example.instacut, PID: 10567
06-28 18:17:16.488: E/AndroidRuntime(10567): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.instacut/com.example.instacut.MainActivity}: java.lang.NullPointerException
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread.access$800(ActivityThread.java:138)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.os.Looper.loop(Looper.java:136)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread.main(ActivityThread.java:5116)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at java.lang.reflect.Method.invokeNative(Native Method)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at java.lang.reflect.Method.invoke(Method.java:515)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at dalvik.system.NativeStart.main(Native Method)
06-28 18:17:16.488: E/AndroidRuntime(10567): Caused by: java.lang.NullPointerException
06-28 18:17:16.488: E/AndroidRuntime(10567):    at com.example.instacut.MainActivity.onCreate(MainActivity.java:41)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.Activity.performCreate(Activity.java:5231)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-28 18:17:16.488: E/AndroidRuntime(10567):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
06-28 18:17:16.488: E/AndroidRuntime(10567):    ... 12 more

我在网上关注了一个网站并且它完全一样。我真的不明白这里有什么不对。 谢谢

2 个答案:

答案 0 :(得分:1)

您收到空指针错误,因为imageView实际上是null个对象。

首先,您发布的代码中存在错误:

   ImageView imageView = (ImageView);
   this.findViewById(R.id.ImageView1);

应该是:

ImageView imageView = (ImageView) this.findViewById(R.id.ImageView1);

删除第一个分号。如果这不是问题,则imageView设置为null,因为它没有正确地与您activity_main.xml文件中的资源相关联。确保在ImageView内宣布android:id="@+id/ImageView1"声明为activity_main.xml,而不是在其他地方声明。如果这没有帮助,请发布文件,我们将帮助您发现错误。

将来,如果您提出Android问题,请确保在帖子中包含任何相关的xml /资源文件。

答案 1 :(得分:0)

请指出MainActivity.Java中第41行是哪一行,因为如果我们知道它会抛出NullPointerException,那么更容易发现错误。