简体   繁体   English

Android:传递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; 我对Android编程和Java一般都是新手,我无法解决导致此错误的原因; 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);" 在下面显示的代码中(靠近第一个代码段的末尾),行"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. 在Android Studio 1.1.0中的文本"com.(name-removed).(app-name-removed).ColourActivity"中给出错误"Expression expected"

(This is inside the class "public class ColourActivity extends Activity" .) (这是在类"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" : 以下是“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;
    }
}

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. 错误消息应该是不言自明的: ...ColourActivity是一个类名,并且类名本身不是Java中的有效表达式。 This is not an android problem, this is a simple syntax error. 这不是一个android问题,这是一个简单的语法错误。

do_output() expects an instance of an Activity . do_output()需要一个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 我假设 - 因为你从你的活动中调用了do_output()你可能想尝试一下

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. 正如@Priya辛格尔建议的,明确提及ColorActivity.this是必要的,因为要调用从内部类内的方法和this指的是内部类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM