I need to resolve the package version from manifest in Android. I do it straight in the main activity, don’t know if it’s wrong.
I can’t test if this works now, for various reasons I wouldn’t be able in a few days despite I’m running Eclipse but this really hurts my mind as I’m a rather worrying person:
{ try {
String version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
} catch (NameNotFoundException e) {
Log.e("tag", e.getMessage());
} }
Do you notice the braces before and after the WHOLE statement? If I delete them, Eclipse will say
“Syntax error on token “{“, { expected after this token”
in the preceding activity opening, and similar stuff in any method or statement after it.
I googled for it because it’s what I can do, and found nothing about them being needed. Java tutorials, samples spell try catch without this. Am I doing something very stupid or is it normal behaviour and my code should run when I’m back?
Again sorry for this question but I just needed.
Thanks in advance.
You probably have that code outside any method. With the braces, Java treats it as an instance initializer block. Without the braces, it’s illegal.
Another thing wrong with that code is that you initialize the variable
versionand then immediately lose the information becauseversionis local to thetryblock. The variable should probably be declared outside the block.