Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在ImageView中设置位图时出现空指针异常_Java_Android_Bitmap - Fatal编程技术网

Java 在ImageView中设置位图时出现空指针异常

Java 在ImageView中设置位图时出现空指针异常,java,android,bitmap,Java,Android,Bitmap,我想从我的移动存储中将图像加载到应用程序的imageview中。为了确保这一点,我将图像保存在内部和外部存储器中。但是在编译时,我得到了一个空指针异常。我还将相同的图像放在drawable文件夹中,并尝试从那里设置imageview。但面临同样的问题。有人能帮我吗!。我正在添加我尝试过的代码片段 方法1: File imageFile = new File(Environment.getExternalStorageDirectory(),"golden_eagle.jpg"); if(imag

我想从我的移动存储中将图像加载到应用程序的imageview中。为了确保这一点,我将图像保存在内部和外部存储器中。但是在编译时,我得到了一个空指针异常。我还将相同的图像放在drawable文件夹中,并尝试从那里设置imageview。但面临同样的问题。有人能帮我吗!。我正在添加我尝试过的代码片段

方法1:

File imageFile = new File(Environment.getExternalStorageDirectory(),"golden_eagle.jpg");
if(imageFile.exists()) {
    Log.v(LOGTAG, "Image file exists");
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap outputImageBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
    ImageView imageView = (ImageView)findViewById(R.id.sample_image);
    imageView.setImageBitmap(outputImageBitmap);
}
方法2:

Bitmap bitimage;
bitimage = BitmapFactory.decodeResource(getResources(), R.drawable.golden_eagle);
if(bitimage == null)
Log.v(LOGTAG,"bitimage is empty");
else
Log.v(LOGTAG,"bitimage is not empty");
ImageView imageView = (ImageView)findViewById(R.id.sample_image);
imageView.setImageBitmap(bitimage);
方法3:

ImageView imageView = (ImageView)findViewById(R.id.sample_image);
int resid = getResources().getIdentifier("golden_eagle","drawable",getPackageName());
imageView.setImageResource(resid);
//imageView.setImageResource(R.drawable.golden_eagle);
方法4:

//Some class extending AppCompatActivity
asyncTask.execute();

//some asyncTask

onpostExecute(){
    //Method:1
    //Method:2
    //Method:3
}
以上所有方法都以相同的编译错误结束

12-24 20:16:42.520 1349-1349/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.home.BequestProto, PID: 1349
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.home.BequestProto/com.example.home.BequestProto.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
    at com.example.home.BequestProto.MainActivity.onCreate(MainActivity.java:75)
    at android.app.Activity.performCreate(Activity.java:6582)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2532)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:207) 
    at android.app.ActivityThread.main(ActivityThread.java:5776) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
错误发生在我将image设置为imageview的行上。 我知道这是一个空指针异常,因为我忘记初始化所需的对象。我想知道是哪一个

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
这似乎来自“方法3”,您在其中调用
setImageResource()

此错误表示此行是您的问题所在:

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

imageView
在所有四种情况下均为
null
,因为
findViewById()
找不到此小部件。

请仔细检查是否有id为
sample\u image
imageView
。在布局中找不到它

“方法4:”显示您正在使用异步任务执行。我猜布局改变了,它试图在之后设置图像

    ImageView imageView = (ImageView)findViewById(R.id.sample_image);
    if (imageView != null)
            imageView.setImageBitmap(outputImageBitmap);
这将仅在视图存在时设置图像

是,findViewById()无法找到此视图,因为我忘了为该布局提供id。感谢帮助。