gpt4 book ai didi

java - 安卓 Java : Modification of instance variable inside button click handler

转载 作者:太空宇宙 更新时间:2023-11-04 15:09:37 24 4
gpt4 key购买 nike

在 Button OnClick() 处理程序中分配/修改类的实例变量有一种奇怪的行为。

如下所示,实例变量“CurrentFile”在 imgBtn.setOnClickListener() 处理程序中进行修改。但是当在 onActivityResult() 方法中访问该变量时,该变量包含值“null”。

根据我的理解,“GetPicActivity” Activity 在Camera Activity 返回之前不会被销毁。因此,实例变量“CurrentFile”不应为空。

如果我缺少一些基础知识,请帮忙。

import java.io.File;
import java.io.IOException;

import com.favoritepics.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class GetPicActivity extends Activity {

protected static final int ID_REQ_IMAGE_CAPTURE = 1;
protected static final int ID_REQ_PICK_PHOTO = 0;
protected File currentFile = null; /// Goal is "to modify this variable inside Button onClick handlers"

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

getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.DKGRAY));

setContentView(R.layout.activity_get_pic);

// set handler image gallery
ImageButton imgBtn = (ImageButton) findViewById(R.id.imgGallery);
imgBtn.setOnClickListener(new ImageButton.OnClickListener() {

@Override
public void onClick(View v) {
// create intent to take picture on camera
Intent pickPhoto = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(pickPhoto, ID_REQ_PICK_PHOTO);
}
});

// set handler for camera image
imgBtn = (ImageButton) findViewById(R.id.imgCamera);
imgBtn.setOnClickListener(new ImageButton.OnClickListener() {

@Override
public void onClick(View v) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempDir = Environment.getExternalStorageDirectory();
File tempFile = null;

try {

currentFile = File
.createTempFile("_jpeg_", ".jpg", tempDir);

} catch (IOException exception) {
Toast.makeText(
GetPicActivity.this,
"Problem occured during creation of temp file! "
+ exception.getMessage(),
Toast.LENGTH_SHORT).show();
}

if (currentFile != null) {
takePicture.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(currentFile));
startActivityForResult(takePicture, ID_REQ_IMAGE_CAPTURE);

}
}
});

// set handler for cancel button
Button btnCancel = (Button) findViewById(R.id.btnCancel);

btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case ID_REQ_IMAGE_CAPTURE:
if (resultCode == RESULT_OK) {
Intent newIntent = new Intent();
newIntent.putExtra("TYPE", requestCode);
newIntent.putExtra("PATH", currentFile.getAbsolutePath());
setResult(RESULT_OK, newIntent);
finish();
}

break;

default:
break;
}
}
}

点击此处查看Logcat messages

我打算做的步骤:

1, Create a temporary file in External storage for the destination of Camera picture

2, Start Camera to take a picture by passing the URI of temporary file

3, OnActivityResult() from camera, return the (temporary) File path to previous activity for further process

但我看到的是,

一旦相机启动并拍摄照片,就会再次创建 GetPicActivity。因此,实例变量“CurrentFile”将为空并且在 OnActivityResult() 中,我无法获取临时文件的文件路径。

为什么启动Camera后GetPicActivity就被销毁了?

最佳答案

启动相机后,相机将以“横向”屏幕方向显示。

甚至现有 Activity 堆栈的方向似乎也更改为“横向”。

所以,当Camera的结果返回到GetPicActivity方法OnActivityResult()时,GetPicActivity被再次创建。但现在,实例变量“CurrentFile”为空。这会给出一个 NullPointerException

关于java - 安卓 Java : Modification of instance variable inside button click handler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21514424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com