java.lang.NullPointerException:尝试调用虚方法' java.lang.Object android.os.Bundle.get(java.lang.String)'在null对象引用上

时间:2016-07-10 20:41:10

标签: java android android-studio

我正在制作一个简单的应用程序来拍摄照片并将其放置在ImageView中,如果我接受图片,它会起作用,但如果我选择不使用图片,我会得到" java.lang.NullPointerException :尝试调用虚方法' java.lang.Object android.os.Bundle.get(java.lang.String)'在null对象引用"。 调试它我可以看到mExtra = null,在那里我实现了一个if \ else语句,但如果我拒绝该图片,应用程序仍然会崩溃。

以下是代码段:

 public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
int scoreTeamB = 0;

Button btnTakePhoto;
ImageView imgTakenPhoto;
private static final int CAM_REQUEST = 1313;
// private static final String EXTRAS_CAMERA_FACING = "android.intent.extras.CAMERA_FACING";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnTakePhoto = (Button) findViewById(R.id.take_left_picture);
    imgTakenPhoto = (ImageView) findViewById(R.id.left_player_image);//left_player_image imageView name

    btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
}

public void imgClick(View view) {
    Toast.makeText(this, "imageview clicked", Toast.LENGTH_SHORT).show();
    Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraintent, CAM_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAM_REQUEST) {

        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

        if (thumbnail == null) {
            //if picture not accepted to be used set the imageView to the star2.png file
            imgTakenPhoto.setImageResource(R.drawable.star2);
            Toast.makeText(this, "no image to include", Toast.LENGTH_SHORT).show();
        } else {
            //if picture accepted use it to populate the imageView
            imgTakenPhoto.setImageBitmap(thumbnail);

        }
    }
}
    class btnTakePhotoClicker implements Button.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraintent, CAM_REQUEST);
        }
    }       

我的问题似乎是if / else语句中的onActivityResult方法,但我没有看到它? 我非常感谢你的帮助, 谢谢, 奥利弗

0 个答案:

没有答案