我的Java代码中的全局变量出现问题。事情就是这样:我的屏幕有3个ImageButtons元素,用于在按下时拾取3个图像;这很好用。我正在使用onActiviyResult来实现它,但是我已经为3个图像实现了一个onActiviyResult方法,所以我在方法中使用3个if(){...}块来知道按下的图像按钮,我的意思是:
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”,具体取决于按下的ImageButton。我认为在调用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;
}
}
答案 0 :(得分:4)
您可以使用不同的请求代码开始活动。
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
}
}
}
}