我在Java代碼中遇到全局變量問題。這裏是一件事:我的屏幕有3個ImageButtons元素用於在按下時選擇3張圖像;這工作正常。我使用onActiviyResult來創建它,但是我爲這3個圖像實現了一個onActiviyResult方法,所以我在方法中使用了3個if(){...}塊來知道按下的按鈕,我的意思是:全局變量值onActivityResult內部沒有改變Android上的方法
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是一個字符串,它的聲明的onCreate方法外,它的默認值設置爲:字符串current_picture =「」;
我使用這個變量來保存這是在3個imagebuttons的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」,具體取決於按下圖像按鈕。我認爲它的價值在調用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;
}
}
的onCreate方法以上,這是一個全局變量,私人字符串current_picture = 「」; –
它看起來相當不錯,爲什麼不在開始意向之前使用loogcat來查看current_picture的值... –
current_picture的值不會改變,它在啓動意向Xoce時仍然是「」。 –