2012-12-10 2 views
1

메시지를 여러 번 표시하기 위해 안드로이드에서 토스트를 사용했지만 전혀 문제가 없었습니다. 그러나 컴파일러가 어떤 이유로이 시간에 작동하도록 허용하지 않습니다. 토스트가이 방법의 내부에 놓이는 것을 허용하지 않는 이유는 무엇입니까?안드로이드 자바 메소드 내에서 토스트 메시지 컴파일 오류가 발생했습니다

이 코드에서는 "ThumbnailsActivity.class"와 "this"두 가지 유형의 컨텍스트를 모두 시도했습니다.

decodeSampleBitmapFromResource 메소드는 Activity를 확장하는 Android 클래스 ThumbnailsActivity.java 안에 있습니다. 여기에는 특별한 것이 없습니다.

public static Bitmap decodeSampledBitmapFromResource(String fileName, int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(fileName, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(fileName, options); 

     // both of the toasts shown here have compile errors 

    Toast.makeText(ThumbnailsActivity.class, "TEST",Toast.LENGTH_LONG).show(); 

     Toast.makeText(this, "TEST",Toast.LENGTH_LONG).show(); 

}//end decodeSampledBitmapfromresource method 
+0

사용 getApplicationContext() .. :) –

+0

시도'ThumbnailsActivity.this' : –

답변

3

낙 용구 당신의 방법으로 :

public Bitmap decodeSampledBitmapFromResource(String fileName, int reqWidth, int reqHeight) { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(fileName, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 

      // both of the toasts shown here have compile errors 

     Toast.makeText(ThumbnailsActivity.this, "TEST",Toast.LENGTH_LONG).show(); 

      Toast.makeText(this, "TEST",Toast.LENGTH_LONG).show(); 

     return BitmapFactory.decodeFile(fileName, options); 



    }//end decodeSampledBitmapfromresource method 

return 문 앞에 넣어 모든 토스트, 또한 비 정적 컨텍스트를 액세스하려면 방법이 정전기 제거

+2

좋은 캐치의 U는 지금 확인 후 getApplicationContext에 쓰기()와

,

Toast.makeText(getApplicationContext(),"TEST",Toast.LENGTH_LONG).show(); 

+0

예,이 작품 , 나는 토스트가 반환 문장 뒤에 있다는 것을 먼저 알지 못했다. – Kevik

+0

왜이 토스트가 return 문 앞에 잘 컴파일되지만 나중에 컴파일되지는 않는지 이해할 수 없습니다. – Kevik

3

당신은 현재 Activity 년대를 호출 할 수 없습니다 static 방법으로부터 직접 Context.

현재 ActivityContext을 param으로 static 메서드에 전달하거나 메서드를 비 정적으로 만들 수 있습니다.

1
대신
관련 문제