-1

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();
            }
        });
Community
  • 1
  • 1

3 Answers3

0

Try this,

public String X = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    Button submit=(Button)findViewById(R.id.submit); 
    submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            class MyAsyncTask extends AsyncTask<DownloadManager.Request, Void, String> {

                @Override
                protected String doInBackground(DownloadManager.Request... params) {
                    try {
                        translatedText = Translate.execute(X, Language.AUTO_DETECT, Language.FRENCH);

                        return translatedText;

                    } catch (Exception e) {
                        e.printStackTrace();
                        return "";
                    }

                    return null;
                }
            }
            new MyAsyncTask().execute();
        }
    });
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
0

Ok, If I understood correctly it should be like that: declare your

public String X = "";

in the outer class, outside of the onClick method. Next you need to declare constructor and member for your MyAsyncTask

private String mTextToTranslate;
public MyAsyncTask(String x){
    mTextToTranslate = x;
}

and in the end use mTextToTranslate instead of X

Raphau
  • 116
  • 5
0

When you implement onClickListener() the way you are doing, in this case an anonymous inner class is implementing this interface and hence you cannot use a variable that is not final this way.

Your AsyncTask should be having a constructor and you should be passing this variable to that constructor and having it stored inside AsyncTask. Then you could go on further with your code without having this problem.

Shoaib Anwar
  • 1,555
  • 12
  • 26