我一直收到这个错误,我不知道为什么。我试过谷歌搜索但没有运气。
04-21 15:43:24.822 2461-2461 / com.example.s.myapplication D / AndroidRuntime:关闭VM 04-21 15:43:24.823 2461-2461 / com.example.s.myapplication E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.s.myapplication,PID:2461 java.lang.RuntimeException:将结果ResultInfo {who = null,request = 1,result = -1,data = null}传递给activity {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) 在android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) 在android.app.ActivityThread.access $ 1300(ActivityThread.java:144) 在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) at java.lang.reflect.Method.invoke(Native 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) 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' 在com.example.s.myapplication.MainActivity.onActivityResult(MainActivity.java:46) 在android.app.Activity.dispatchActivityResult(Activity.java:6139) 在android.app.ActivityThread.deliverResults(ActivityThread.java:3535) 在android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) 在android.app.ActivityThread.access $ 1300(ActivityThread.java:144) 在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) at java.lang.reflect.Method.invoke(Native 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) 一个
这是我的主要活动。该应用程序的目的是:
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);
}
}
答案 0 :(得分:4)
imageView = (ImageView)findViewById(R.id.imageView2);
答案 1 :(得分:1)
这对我有用:
我写错了我的imageView的ID。应该是imageView2
imageView = findViewById(R.id.imageView2);
答案 2 :(得分:0)
如果上述解决方案对您不起作用
oncreate方法再次应该是这样
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);imageView=findViewById(R.id.imageView);
setContentView(R.layout.activity_main);
imageView=findViewById(R.id.imageView);
}