gpt4 book ai didi

java - Android 上的 onActivityResult 方法内的全局变量值未更改

转载 作者:行者123 更新时间:2023-12-01 18:06:18 25 4
gpt4 key购买 nike

我的 Java 代码中的全局变量出现问题。事情是这样的:我的屏幕有 3 个 ImageButtons 元素,按下时可以选取 3 个图像;这很好用。我使用 onActiviyResult 来实现它,但我只为 3 个图像实现了一个 onActiviyResult 方法,因此我在方法中使用 3 个 if(){...} block 来了解按下的图像按钮,我的意思是:

if(current_picture.equals("pic1"))}{
imagebutton1.setImageBitmap(bitmap);
}

if(current_picture.equals("pic2"))}{
imagebutton2.setImageBitmap(bitmap);
}

if(current_picture.equals("pic3"))}{
imagebutton3.setImageBitmap(bitmap);
}

这里,current_picture是一个String,它是在onCreate方法之外声明的,它的默认值设置为:String current_picture = "";

我使用此变量来保存在 3 个图像按钮的 setonclicklistener 事件上设置的值,我的意思是:

imagebutton1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
current_picture = "pic1";
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose a
picture"), SELECT_PICTURE);


}
});

imagebutton2 (current_picture = "pic2";) 和 imagebutton3 (current_picture = "pic3";) 也是如此。显然,所有这些事件都在 onCreate 方法上。

所以,问题是,在调用 onActivityResult 方法时,current_picture 正在丢失其在 setonclicklistener 方法上设置的值,我的意思是,current_user 值仍然是“”,而不是“pic1”、“pic2”或“pic3”,具体取决于按下的按钮图像按钮。我认为它的值在调用新 Activity onActivityResult时被破坏,然后onActivityResul只识别:String current_picture = "";

我做了很多事情来解决这个问题,但我能够找到解决方案,我在下面附加了一些代码(不是全部,只是重要部分):

public class Publish_Event extends Activity{

private ImageButton imagebutton1;
private ImageButton imagebutton2;
private ImageButton imagebutton3;
private Bitmap bitmap;
private String current_picture="";


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.publicar_eventos);

StrictMode.ThreadPolicy p = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(p);

imagebutton1 = (ImageButton)findViewById(R.id.pic1);
imagebutton2 = (ImageButton)findViewById(R.id.pic2);
imagebutton3 = (ImageButton)findViewById(R.id.pic3);


imagebutton1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
current_picture = "pic1";
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose a
picture"), SELECT_PICTURE);


}
});


imagebutton2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
current_picture = "pic2";
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose a
picture"), SELECT_PICTURE);


}
});

imagebutton3.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
current_picture = "pic3";
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose a
picture"), SELECT_PICTURE);


}
});


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
imagen_path = getRealPathFromURI(selectedImageUri);
bitmap = BitmapFactory.decodeFile(imagen_path);




if(current_picture.equals("pic1")){

imagebutton1.setImageBitmap(bitmap);


}

if(current_picture.equals("pic2")){

imagebutton2.setImageBitmap(bitmap);


}

if(current_picture.equals("pic3")){

imagebutton3.setImageBitmap(bitmap);


}



}
}
}

@TargetApi(Build.VERSION_CODES.KITKAT)
public String getRealPathFromURI(Uri contentUri) {


String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = null;
try {
if (Build.VERSION.SDK_INT > 19) {
// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(contentUri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";

cursor = Publish_Event.this.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, sel, new String[] { id }, null);
} else {
cursor =

Publish_Event.this.getContentResolver().query(contentUri,
projection, null, null, null);
}
} catch (Exception e) {
e.printStackTrace();
}

String path = null;
try {
int column_index =
cursor.getColumnIndex(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index).toString();
cursor.close();
} catch (NullPointerException e) {
e.printStackTrace();
}
return path;
}


}

最佳答案

您可以使用不同的请求代码启动 Activity 。

public class Publish_Event extends Activity{
private static final int SELECT_PICTURE_1 = 1;
private static final int SELECT_PICTURE_2 = 2;
private static final int SELECT_PICTURE_3 = 3;

protected void onCreate(Bundle savedInstanceState) {
imagebutton1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose a picture"), SELECT_PICTURE_1);
}
});
imagebutton2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose a picture"), SELECT_PICTURE_2);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_PICTURE_1) {
//change imagebutton1
}else if(requestCode == SELECT_PICTURE_2){
//change imagebutton2
}else if(requestCode == SELECT_PICTURE_3){
//change imagebutton3
}
}
}

}

关于java - Android 上的 onActivityResult 方法内的全局变量值未更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36184758/

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