Java 尝试在空对象引用上调用虚拟方法“void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29777098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
提问by smither123
I keep getting this error and i'm not sure why. I've tried googling it but no luck there.
我一直收到这个错误,我不知道为什么。我试过谷歌搜索,但没有运气。
04-21 15:43:24.822 2461-2461/com.example.s.myapplication D/AndroidRuntime﹕ Shutting down VM 04-21 15:43:24.823 2461-2461/com.example.s.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.s.myapplication, PID: 2461 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.s.myapplication/com.example.s.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:3539) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) at android.app.ActivityThread.access$1300(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference at com.example.s.myapplication.MainActivity.onActivityResult(MainActivity.java:46) at android.app.Activity.dispatchActivityResult(Activity.java:6139) at android.app.ActivityThread.deliverResults(ActivityThread.java:3535) ????????????at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) ????????????at android.app.ActivityThread.access$1300(ActivityThread.java:144) ????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) ????????????at android.os.Handler.dispatchMessage(Handler.java:102) ????????????at android.os.Looper.loop(Looper.java:135) ????????????at android.app.ActivityThread.main(ActivityThread.java:5221) ????????????at java.lang.reflect.Method.invoke(Native Method) ????????????at java.lang.reflect.Method.invoke(Method.java:372) ????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) ????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) a
04-21 15:43:24.822 2461-2461/com.example.s.myapplication D/AndroidRuntime﹕关闭虚拟机 04-21 15:43:24.823 2461-2461/com.example.s.myapplication E/AndroidRuntime﹕致命例外:主进程:com.example.s.myapplication,PID:2461 java.lang.RuntimeException:未能将结果 ResultInfo{who=null, request=1, result=-1, data=null} 传递给活动 {com.example .s.myapplication/com.example.s.myapplication.MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚方法“void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)” android.app.ActivityThread.deliverResults(ActivityThread.java:3539) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) at android.app。ActivityThread.access$1300(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper。 loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method. java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 由:java.lang .NullPointerException: 尝试调用虚方法 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)'在 com.example.s.myapplication.MainActivity.onActivityResult(MainActivity.java:46) at android.app.Activity.dispatchActivityResult(Activity.java:6139) at android.app.ActivityThread.deliverResults(ActivityThread. java:3535) ????????????at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) ????????????at android.app.ActivityThread。 access$1300(ActivityThread.java:144) ????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) ????????????在 android.os.Handler.dispatchMessage(Handler.java:102) ???????????? 在 android.os.Looper.loop(Looper.java:135) ???????????? ????在android.app.ActivityThread.main(ActivityThread.java:5221) ????????????在java.lang.reflect.Method。调用(本机方法)????????????在 java.lang.reflect.Method.invoke(Method.java:372) ????????????在 com.android .internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) ???????????? 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) a
This is my main activity. What the application is meant to do is:
这是我的主要活动。该应用程序的目的是:
- Take a photo
- Save the photo in a tmp location
- load the photo into a imageview
- 拍个照
- 将照片保存在 tmp 位置
- 将照片加载到图像视图中
.
.
package com.example.s.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
public class MainActivity extends Activity {
static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openCamera();
setContentView(R.layout.activity_main);
}
public void openCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//Check that request code matches ours:
if (requestCode == REQUEST_IMAGE_CAPTURE) {
//Get our saved file into a bitmap object:
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
Bitmap image = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
imageView.setImageBitmap(image);
}
}
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {
//First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize, Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight) {
inSampleSize = Math.round((float) height / (float) reqHeight);
}
int expectedWidth = width / inSampleSize;
if (expectedWidth > reqWidth) {
//if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float) width / (float) reqWidth);
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public void capture_btn(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
回答by smither123
imageView = (ImageView)findViewById(R.id.imageView2);
回答by saurabh
if above solutions did not work for you check
如果上述解决方案对您不起作用,请检查
oncreate method again it should be like this
再次 oncreate 方法应该是这样的
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);imageView=findViewById(R.id.imageView);
setContentView(R.layout.activity_main);
imageView=findViewById(R.id.imageView);
}