Java ImageView中的空指针异常

Java ImageView中的空指针异常,java,android,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.gr

我正在尝试使用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>
我已调试并检查ImageView是否为空,但无法找出原因/方式。请有人澄清我的疑问。请提前感谢简单的范围问题

这条线

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

imageView = (ImageView) findViewById(R.id.result);
为什么??因为您现在这样做意味着您已经声明了一个新的imageView,而不是使用之前已经声明的imageView。您已经保留了先前声明的imageView,而创建了一个全新的,在
onCreate()
完成后完全无用且消失的imageView。调用
imageView.setImageBitmap(位图)时,您将得到一个NPE,因为它试图使用类中声明的imageView。

简单的范围问题

这条线

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

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

为什么??因为您现在这样做意味着您已经声明了一个新的imageView,而不是使用之前已经声明的imageView。您已经保留了先前声明的imageView,而创建了一个全新的,在
onCreate()
完成后完全无用且消失的imageView。调用
imageView.setImageBitmap(位图)时
,您将得到一个NPE,因为它试图使用类中声明的imageView。

在您的
onCreate
方法中,您通过以下方式创建局部变量:-

ImageView imageView = (ImageView) findViewById(R.id.result);
这将隐藏全局
imageView
成员。 因此,请在
onCreate
中执行以下操作:-

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

空指针应该会消失。

在您的
onCreate
方法中,您正在通过以下方式创建局部变量:-

ImageView imageView = (ImageView) findViewById(R.id.result);
这将隐藏全局
imageView
成员。 因此,请在
onCreate
中执行以下操作:-

imageView = (ImageView) findViewById(R.id.result);
空指针应该是空的