android活动结果空指针异常

时间:2014-12-18 00:59:57

标签: android camera onactivityresult

我正在创建一个Android应用程序,需要几张照片是样本。我在onCreate方法中有以下内容。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // remove title
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_sampling);

    initLocalVariables();
}

private void initLocalVariables() {
    this.sampleView = (ImageView) findViewById(R.id.sampleView);
    this.startButton = (Button) findViewById(R.id.startSamplingButton);

    this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 1, this);
    this.currentLocation = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    this.paintBrush = new Paint();
    this.paintBrush.setStyle(Style.FILL);

    this.companyId = 0;
    this.fieldId = 0;
    this.plantSetId = 0;
    this.standardRadius = 10f;
    this.colorSelected = null;
    this.sampleView.setOnTouchListener(this);
    this.isSelectingPoint = false;

    this.configureSample();

}

initLocalVariables方法初始化局部变量和对象,并调用以下方法。

private void configureSample() {
    if (this.sampleList == null) {
        this.sampleList = new ArrayList<Muestra>();
    }

    if (this.sampleList.size() == 0) {
        this.currentSample = new Muestra();
        this.currentSample.setIdMuestra(1);
        this.currentSample.setIdEmpresa(this.companyId);
        this.currentSample.setIdCampo(this.fieldId);
        this.currentSample.setIdPanyo(this.plantSetId);
    } else {
        this.currentSample = this.currentSample.obtenerSiguienteMuestra();

        if (this.currentLocation != null) {
            this.currentSample.setUbicacion(new GeoLocation(this.currentLocation.getLatitude(), this.currentLocation.getLongitude(), this.currentLocation.getAltitude(),
                    this.currentLocation.getAccuracy()));
        }
    }
}

如您所见,currentSample不为null。我要求系统拍这样的照片。

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

        this.currentPicture = this.currentSample.obtenerSiguienteImagenMuestra(directorioAlmacenamiento);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, this.currentPicture.obtieneUriDesdeArchivo());
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

我拍照并请求保存在文件系统中。图片已保存,但在完成保存后,应用程序将抛出以下异常。

12-17 21:47:23.505:E / AndroidRuntime(15493):致命异常:主要 12-17 21:47:23.505:E / AndroidRuntime(15493):java.lang.RuntimeException:无法恢复活动{com.agrobotica.agroberries / com.agrobotica.agroberries.SamplingActivity}:java.lang.RuntimeException:传递失败结果ResultInfo {who = null,request = 1,result = -1,data = null}到activity {com.agrobotica.agroberries / com.agrobotica.agroberries.SamplingActivity}:java.lang.NullPointerException 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2919) 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948) 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354) 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.app.ActivityThread.access $ 700(ActivityThread.java:159) 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1316) 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.os.Handler.dispatchMessage(Handler.java:99) 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.os.Looper.loop(Looper.java:176) 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.app.ActivityThread.main(ActivityThread.java:5419) 12-17 21:47:23.505:E / AndroidRuntime(15493):at java.lang.reflect.Method.invokeNative(Native Method) 12-17 21:47:23.505:E / AndroidRuntime(15493):at java.lang.reflect.Method.invoke(Method.java:525) 12-17 21:47:23.505:E / AndroidRuntime(15493):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046) 12-17 21:47:23.505:E / AndroidRuntime(15493):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 12-17 21:47:23.505:E / AndroidRuntime(15493):at dalvik.system.NativeStart.main(Native Method) 12-17 21:47:23.505:E / AndroidRuntime(15493):引起:java.lang.RuntimeException:将结果ResultInfo {who = null,request = 1,result = -1,data = null}传递给activity { com.agrobotica.agroberries / com.agrobotica.agroberries.SamplingActivity}:java.lang.NullPointerException 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.app.ActivityThread.deliverResults(ActivityThread.java:3500) 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2906) 12-17 21:47:23.505:E / AndroidRuntime(15493):... 12更多 12-17 21:47:23.505:E / AndroidRuntime(15493):引起:java.lang.NullPointerException 12-17 21:47:23.505:E / AndroidRuntime(15493):at com.agrobotica.agroberries.SamplingActivity.onActivityResult(SamplingActivity.java:231) 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.app.Activity.dispatchActivityResult(Activity.java:5563) 12-17 21:47:23.505:E / AndroidRuntime(15493):在android.app.ActivityThread.deliverResults(ActivityThread.java:3496) 12-17 21:47:23.505:E / AndroidRuntime(15493):... 13更多

调试时我没有访问onActivityResult。这是我的onActivityResult。

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

        if (this.currentLocation == null) {
            this.currentLocation = this.locationManager.getLastKnownLocation(LOCATION_SERVICE);
        }

        if (this.currentLocation != null) {
            this.currentSample.setUbicacion(new GeoLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), currentLocation.getAltitude(), currentLocation
                    .getAccuracy()));
        }

        Bitmap bitmap = currentPicture.convertToAndroidBitmap();
        this.imageCanvas = new Canvas();
        this.imageCanvas.setBitmap(bitmap);
        this.sampleView.setImageBitmap(bitmap);
    }
}

要开始采样过程,请执行以下操作。

public void onClickStartSampling(View v) {
    this.startButton.setVisibility(View.GONE);
    this.sampleView.setVisibility(View.VISIBLE);

    dispatchTakePictureIntent();
}

有什么想法吗?

感谢!!!

1 个答案:

答案 0 :(得分:0)

解决!检查手机中的显影选项我发现已经检查了“没有保持活动”#34;在选项列表的末尾。这是在启动相机活动后关闭活动,因此当从保存图片返回时,活动为空。