Mojo Jojo
Mojo Jojo

Reputation: 173

Warning This method has a constructor name

I am using a class named A in android development, this warning pops out when I create a class named:

private Intent a() // This method has a constructor name
{
    b.setDrawingCacheEnabled(true);
    Bitmap bitmap = b.getDrawingCache();
    Intent intent = new Intent("android.intent.action.SEND");
    intent.setType("image/jpg");
    ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
    bitmap.compress(android.graphics.Bitmap.CompressFormat.JPEG, 100,
            bytearrayoutputstream);
    File file = new File((new StringBuilder())
            .append(Environment.getExternalStorageDirectory())
            .append(File.separator).append("temp_file.jpg").toString());
    try {
        file.createNewFile();
        FileOutputStream fileoutputstream = new FileOutputStream(file);
        fileoutputstream.write(bytearrayoutputstream.toByteArray());
        fileoutputstream.close();
    } catch (IOException ioexception) {
        ioexception.printStackTrace();
    }
    intent.addFlags(0x80000);
    intent.putExtra("android.intent.extra.STREAM", Uri.fromFile(file));
    a.startActivity(Intent.createChooser(intent, "Share via: "));
    return intent;
}

what should I do?

Upvotes: 0

Views: 6854

Answers (2)

Gio
Gio

Reputation: 3340

You are seeing this warning because the Android lint tool (static code analysis tool) that checks your Android project source files for potential bugs and optimization improvements detects a suspicious (but not erroneous) naming here.

You have 3 options, in all cases everything will compile fine and nothing will explode. I've ordered the options from best to worse.

  1. Make everybody happy and change the name

  2. Keep using the ridiculous name and ignore the warning

  3. Make the warning disappear by adding an @supresswarnings rule above the part where it is giving the warning or adjust the lint settings in your IDE.

Upvotes: 1

nhaarman
nhaarman

Reputation: 100438

What @Raghunandan said.

Creating methods named the same as the classname (thus constructor) are apparently not necessarily illegal (warning, no error?), but can be very confusing. Try to come up with a more descriptive name of your method.

Upvotes: 2

Related Questions