Android targetVersion28下调用带参canvas.save()报错,求解决方案
Hey there! Let's break down why you're hitting this error and how to fix it quickly.
Why the Error Occurs
The @SaveFlags you’re trying to pass as an argument is an annotation, not an integer value. Annotations act as metadata for the compiler or IDE—they tell you which integer constants are valid for this method—but you can’t pass an annotation directly as a method parameter. That’s a syntax violation, which is exactly why you’re seeing the "illegal start of expression" error.
In API 28, the Canvas class has two save() methods:
save(): No parameters, saves the entire canvas statesave(int saveFlags): Accepts an integer flag to specify exactly which parts of the state to save
The @SaveFlags annotation is just a hint for valid input values, not a value you can pass directly to the method.
Step-by-Step Solution
Replace the annotation with the actual integer constant you want to use. Here’s how to adjust your code:
Wrong (your current code):
save = canvas.save(@Saveflags save);
Correct:
Pick the appropriate flag constant from the Canvas class and pass it as an integer:
// Example: Save all canvas state int save = canvas.save(Canvas.ALL_SAVE_FLAG); // Or save only the clip region: int save = canvas.save(Canvas.CLIP_SAVE_FLAG);
Extra Notes
- All valid
saveFlagsconstants are defined directly in theCanvasclass (likeMATRIX_SAVE_FLAG,CLIP_TO_LAYER_SAVE_FLAG), so you don’t need annotations to use them—just reference the constant directly. - The
save(int)method has existed since API Level 1, so it’s fully compatible with your targetVersion 28.
内容的提问来源于stack exchange,提问作者Durga

