Android:传递Activity类时的“表达式预期”

时间:2015-04-25 18:43:39

标签: java android class android-activity

我对Android编程和Java有点陌生,我无法解决导致此错误的原因;据我所知,这应该有效。在下面显示的代码中(靠近第一个代码段的末尾),行"ColourOutput.do_output((Activity) com.(name-removed).(app-name-removed).ColourActivity);"在Android Studio 1.1.0中的文本"Expression expected"上显示错误"com.(name-removed).(app-name-removed).ColourActivity"

(这是在班级"public class ColourActivity extends Activity"内。)

        private Camera.PreviewCallback preview_callback = new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            int width = mCamera.getParameters().getPreviewSize().width;
            int height = mCamera.getParameters().getPreviewSize().height;
            int raw_pixels[];
            int pixels[];

            raw_pixels = new int[width * height];
            pixels = new int[get_sample_width() * get_sample_height()];
            convert_yuv(raw_pixels, data, width, height);
            crop_pixels(raw_pixels, pixels, width, height, (width - get_sample_width()) / 2, (height - get_sample_height()) / 2, get_sample_width(), get_sample_height());

            if (PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("use_mean", true) == true) {
                ColourOutput.add_colour_to_output(
                        ColourTools.get_mean(
                                pixels, get_sample_width(), get_sample_height(),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("detect_secondary", true),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("detect_white", true),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("detect_brightness", true),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("quantization_secondary", 32),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("quantization_dark", 43),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("quantization_light", 128),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("quantization_matching_only", true)
                        ),
                        ColourOutput.ColourType.MEAN);
            }
            if (PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("use_mode", true) == true) {
                ColourOutput.add_colour_to_output(
                        ColourTools.get_mode(
                                pixels, get_sample_width(), get_sample_height(),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("detect_secondary", true),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("detect_white", true),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("detect_brightness", true),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("quantization_secondary", 32),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("quantization_dark", 43),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("quantization_light", 128),
                                PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("quantization_matching_only", true)
                        ),
                        ColourOutput.ColourType.MODE);
            }
            ColourOutput.do_output((Activity) com.(name-removed).(app-name-removed).ColourActivity);
        }
    };

以下是“ColourOutput.do_output”的定义

public class ColourOutput {
    private static boolean output_clear = true;
    private static String output_buffer = "";

    public static enum ColourType {
        MEAN,
        MODE
    }

    public static void add_colour_to_output(ColourTools.ColourDescription colour, ColourType type) {
        if (output_clear == true) {
            output_buffer = colour.Brightness.toString() + " " + colour.Colour.toString();
        }
        else {
            output_buffer = output_buffer + " " + colour.Brightness.toString() + " " + colour.Colour.toString();
        }
        output_clear = false;
    }

    public static void do_output(Activity activity) {
        ((TextView) activity.findViewById(R.id.output_text)).setText(output_buffer);
        output_buffer = "";
        output_clear = true;
    }
}

1 个答案:

答案 0 :(得分:5)

错误消息应该是不言自明的:...ColourActivity是类名,并且类名本身不是Java中的有效表达式。这不是一个Android问题,这是一个简单的语法错误。

do_output()需要Activity实例。我不太确定,你试图通过传递活动类的名称来实现的目标。

我假设 - 因为你在活动中呼叫do_output() - 你可能想尝试

ColourOutput.do_output(ColorActivity.this);

代替。

正如@Priya Singhal所建议的那样,当您从内部类中调用该方法时,ColorActivity.this的显式引用是必需的,this引用该内部类的实例。