I am trying to use a global variable in my onClick() method. I have an AsyncTask() that uses the variable (BTW, I am using Microsoft Translator API and apparently in order to use Translate.execute I need to use AsyncTask()). However, whenever I try to use it, an error pops up, saying:
Variable "[variable]" is accessed from inner class, needs to be declared final.
However, my program requires that I change the variable later on (I'm not done yet, but I'm sure I will need to change the variable), so I can't declare it as final. I read from Variable is accessed within inner class. Needs to be declared final that you should globalize the variable.
Apparently, according to Global variables in Java, you need to use public and static. But here's where I run into a problem. I run into this error when I try to globalize the variable:
Modifier "public" not allowed here
Modifier "static" not allowed here
Is there any way I can use the variable in a different scope and change it whenever my button is pressed? My code is:
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String X = "";
class MyAsyncTask extends AsyncTask<DownloadManager.Request, Void, String> {
try {
translatedText = Translate.execute(X, Language.AUTO_DETECT, Language.FRENCH);
return translatedText;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}
new MyAsyncTask().execute();
}
});