ImageView中的空指针异常

时间:2015-05-28 17:46:54

标签: java android

我正在尝试使用ImageView从手机显示图像。 这是我的ImageActivity活动:

package com.example.apptest1;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View; 
import android.widget.ImageView;

public class ImageActivity extends Activity {

private static final int REQUEST_CODE = 0;
private Bitmap bitmap;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);
     ImageView imageView = (ImageView) findViewById(R.id.result);

}

public void pickImage(View View) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, REQUEST_CODE);
}

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        InputStream stream = null;
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
          try {
            // recyle unused bitmaps
            if (bitmap != null) {
              bitmap.recycle();
            }
            stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);

            imageView.setImageBitmap(bitmap);
          } catch (FileNotFoundException e) {
            e.printStackTrace();
          } finally {
            if (stream != null)
              try {
                stream.close();
              } catch (IOException e) {
                e.printStackTrace();
              }
          }
      }



 }

这是activity_image.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" 
android:orientation="vertical">


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="pickImage"
    android:text="Button" >
</Button>

<ImageView
    android:id="@+id/result"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
</ImageView>


</LinearLayout>

但是当我尝试运行应用程序时,它会显示下面的logcat并突然停止 logcat的:

    05-28 23:11:48.226: E/AndroidRuntime(6115): FATAL EXCEPTION: main
05-28 23:11:48.226: E/AndroidRuntime(6115): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,  request=0, result=-1, data=Intent { dat=content://media/external/images/media/1242 flg=0x1 }} to activity   {com.example.apptest1/com.example.apptest1.ImageActivity}: java.lang.NullPointerException
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3525)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3568)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.access$1100(ActivityThread.java:162)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.os.Handler.dispatchMessage(Handler.java:107)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.os.Looper.loop(Looper.java:194)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.main(ActivityThread.java:5371)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at java.lang.reflect.Method.invoke(Method.java:525)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run   (ZygoteInit.java:833)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at dalvik.system.NativeStart.main(Native Method)
05-28 23:11:48.226: E/AndroidRuntime(6115): Caused by: java.lang.NullPointerException
05-28 23:11:48.226: E/AndroidRuntime(6115):     at com.example.apptest1.ImageActivity.onActivityResult(ImageActivity.java:48)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.Activity.dispatchActivityResult(Activity.java:5311)
05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3521)
05-28 23:11:48.226: E/AndroidRuntime(6115):     ... 11 more

我已经调查并检查过ImageView是否为空但无法弄清楚为什么/如何。有人请清除我的怀疑。谢谢提前

2 个答案:

答案 0 :(得分:3)

简单的范围问题。

这一行

ImageView imageView = (ImageView) findViewById(R.id.result);

需要

imageView = (ImageView) findViewById(R.id.result);

为什么呢?因为你现在这样做的方式意味着你已经声明了一个新的imageView而不是使用之前已经声明的那个。你已经离开了之前声明的imageView,而是在onCreate()完成之后创建了一个绝对无用且消失的全新的。当您致电imageView.setImageBitmap(bitmap);时,您将获得一个NPE,因为它会尝试使用您班级中声明的imageView。

答案 1 :(得分:1)

onCreate方法中,您将通过以下方式创建本地变量: -

ImageView imageView = (ImageView) findViewById(R.id.result);

隐藏了您的全球imageView成员。 所以,请在onCreate下面执行: -

imageView = (ImageView) findViewById(R.id.result);

空指针应该去。