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
4

3 に答える 3

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 ステートメントの前にすべてのトーストを配置し、メソッドから静的も削除します

于 2012-12-10T07:27:14.307 に答える
3

メソッドから currentActivityContext直接呼び出すことはできません。static

current を param としてメソッドに渡すか、メソッドを非静的にすることがActivityできContextますstatic

于 2012-12-10T07:27:50.837 に答える
0

U は getApplicationContext() に書き込み、今すぐチェックします。

Toast.makeText(getApplicationContext(),"TEST",Toast.LENGTH_LONG).show();
于 2012-12-10T07:28:37.477 に答える