Java Android:传递活动类时“预期的表达”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29869191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:43:56  来源:igfitidea点击:

Android: "Expression expected" when passing an Activity class

javaandroidclassandroid-activity

提问by Micheal Johnson

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.

总的来说,我对 Android 编程和 Java 有点陌生,我无法弄清楚是什么导致了这个错误;据我了解,这应该有效。在下面显示的代码中(靠近第一个代码段的末尾),该行在Android Studio 1.1.0 中的文本中"ColourOutput.do_output((Activity) com.(name-removed).(app-name-removed).ColourActivity);"给出了错误。"Expression expected""com.(name-removed).(app-name-removed).ColourActivity"

(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;
    }
}

采纳答案by dhke

The error message should be self-explanatory: ...ColourActivityis 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.

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

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

do_output()期待一个实例Activity。我不太确定,你试图通过传递活动类的名称来实现什么。

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.thisis necessary as you are calling the method from within an inner class and thisrefers to the instance of that inner class.

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