2016-03-23 46 views
0

我在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; 
    } 


} 
+0

的onCreate方法以上,這是一個全局變量,私人字符串current_picture = 「」; –

+0

它看起來相當不錯,爲什麼不在開始意向之前使用loogcat來查看current_picture的值... –

+0

current_picture的值不會改變,它在啓動意向Xoce時仍然是「」。 –

回答

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 
      } 
     } 
    } 

}

+0

它的工作,但我仍然有一個問題,我已經設置了一個默認圖像imagebutton1,2和3,這是相同的圖像,如果我按imagebutton1並選擇圖像,然後它工作正常,新的圖像替換默認的,但如果我按下imagebutton2或imagebutton3並選擇圖像,然後imagebutton1的新圖像(以前選擇)消失並出現默認的一個,同樣的事情發生,如果我首先按imagebutton2或imagebutton3。我不知道是什麼導致了這種行爲 –

+0

我正在實現這個:Uri selectedImageUri = data.getData(); imagen_path = getRealPathFromURI(selectedImageUri); bitmap = BitmapFactory.decodeFile(imagen_path); if(resultCode == Activity.RESULT_OK) –

+0

可能是你的圖片尺寸太大,操作系統需要額外的內存並殺死你的活動,當選擇圖像完成後,android會重新創建活動。隨着current_picture也同樣的問題。嘗試選擇小圖片。 –