Search code examples
javaandroidclassandroid-activity

Android: "Expression expected" when passing an Activity class


I am kind of new to Android programming and Java in general, and I cannot work out what is causing this error; as I understand it this should work. In the code shown below (near the end of the first snippet), the line "ColourOutput.do_output((Activity) com.(name-removed).(app-name-removed).ColourActivity);" is giving the error "Expression expected" on the text "com.(name-removed).(app-name-removed).ColourActivity" in Android Studio 1.1.0.

(This is inside the class "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);
        }
    };

Here is the definition of "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;
    }
}

Solution

  • The error message should be self-explanatory: ...ColourActivity is a class name and a class name by itself is not a valid expression in Java. This is not an android problem, this is a simple syntax error.

    do_output() expects an instance of an Activity. I'm not quite sure, what you where trying to achieve by trying to pass the name of the activity class.

    I'd assume, that --since you are calling do_output() from inside your activity-- you might want to try

    ColourOutput.do_output(ColorActivity.this);
    

    instead.

    As suggested by @Priya Singhal, the explicit reference to ColorActivity.this is necessary as you are calling the method from within an inner class and this refers to the instance of that inner class.