2013-04-10 2 views
0

나는 안드로이드를 처음 접했다. 내가 뭘하려는 건 바보 야?활동에 의도를 세우고 조각에서받는 방법

  //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)에서 수신을 시도하고 아래와 같이 다음과 같이 나는 활동 (EditPhoto)에 의도를 구축해야

   // 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); 

조각 클래스가 정적이므로 "유형 액티비티에서 비 정적 메서드 getIntent()에 정적 참조를 만들 수 없습니다"라고 표시됩니다.

번들을 사용하고 프래그먼트에 인수를 설정했지만 동일한 문제가 다시 발생했습니다.

는 또한, 내가 getActivity을 시도(). getIntent .... 또한이 같은 getActivity 캐스팅 ((ImportPhoto) getActivity()). getIntent ... 두 방법은 응용 프로그램을 실행하지만, 그것을 충돌합니다.

모든 종류의 도움을 주실 수 있습니다. 미리 감사드립니다.

답변

0

당신은 파편에 의도를 보내지 마십시오. 당신이 조각을 만들 때

당신이 할이

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

같은 당신이 조각이

FirstFragment first = (FirstFragment)fragment; 
처럼 조각에 클래스를 캐스팅하여 조각을 확장하는 클래스의 인스턴스를 생성 생성

FirstFragment 클래스에서 방금 생성 한 first 값을 사용하여 사진 또는 기타 광고에 액세스하는 방법을 만듭니다.

first.setPhoto(image); 

액티비티와 통신하기 위해 조각이 필요할 때마다 조각에서 액티비티에 대한 콜백을 생성해야합니다.

관련 문제