2013-04-10 20 views
0

我是Android的新手。我想要做的是愚蠢的? 我有一個活動(EditPhoto)建立一個意向如下:如何建立活動的意圖和從片段接收​​

  //Defining intent for loading image to the edit page 
      Intent recentPhoto = new Intent(this, ImportPhoto.class); 

      //Defining byte stream of image chosen 
      ByteArrayOutputStream bs = new ByteArrayOutputStream(); 
      bm.compress(Bitmap.CompressFormat.PNG, 50, bs); 

      //Transforming image to EditPhoto class in byte stream 
      recentPhoto.putExtra("byteArray", bs.toByteArray()); 

      //Starting the intent 
      startActivity(recentPhoto); 

,我試圖從另一個活動(ImportPhoto)片段(FirstFragment)收到如下圖所示:

   // Getting the image back imported from EditPhoto page 
       final Bitmap photo = BitmapFactory.decodeByteArray(getIntent(). 
            getByteArrayExtra("byteArray"), 
         0,getIntent().getByteArrayExtra("byteArray").length); 

     //Displaying the image in the image viewer 
     viewGalleryImages.setImageBitmap(photo); 

由於片段類是靜態的,它說「無法對類型爲Activity」的非靜態方法getIntent()進行靜態引用。

我嘗試使用捆綁並將參數設置爲fragment,但又遭受同樣的問題。 ((ImportPhoto)getActivity())。getIntent ...這兩種方式運行的應用程序,但崩潰它。

任何形式的幫助將不勝感激。 在此先感謝。

回答

0

你會發現這個錯誤,你不發送意圖碎片。

當您創建的片段,你做這樣的事

Fragment fragment = new FirstFragment(); 
fragTransaction.add(fragment,null).commit; 

當您創建片段創建由類鑄件的片段像這樣

FirstFragment first = (FirstFragment)fragment; 
擴展片段的類的實例

然後在您的FirstFragment類中創建一種方法來拍攝照片或使用您剛創建的first值訪問的任何廣告

first.setPhoto(image); 

任何時候你需要一個片段與您需要創建一個回調,從片段中的活動的活動上進行通信片段閱讀起來here