The findViewById (int) method is undefined

advertisements

I'm new to Android development and I'm trying to code a little app which allows me to grab an external JSON file and parse it. I got this to work, however it wont work if I try to execute it in the background as an AsyncTask. Eclipse gives me the error

The method findViewById(int) is undefined for the type LongOperation

in this line:

TextView txtView1 = (TextView)findViewById(R.id.TextView01);

Here is my code:

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new LongOperation().execute();
    }
}

class LongOperation extends AsyncTask<String, Void, String> {
    private final Context LongOperation = null;

    @Override
    protected String doInBackground(String... params) {
        try {
            URL json = new URL("http://www.corps-marchia.de/jsontest.php");
            URLConnection tc = json.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                    JSONArray ja = new JSONArray(line);
                    JSONObject jo = (JSONObject) ja.get(0);
                    TextView txtView1 = (TextView)findViewById(R.id.TextView01);
                    txtView1.setText(jo.getString("text") + " - " + jo.getString("secondtest"));
            }
        } catch (MalformedURLException e) {
            Toast.makeText(this.LongOperation, "Malformed URL Exception: " + e, Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Toast.makeText(this.LongOperation, "IO Exception: " + e, Toast.LENGTH_LONG).show();
        } catch (JSONException e) {
            Toast.makeText(this.LongOperation, "JSON Exception: " + e, Toast.LENGTH_LONG).show();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
    }
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        ProgressDialog pd = new ProgressDialog(LongOperation);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage("Working...");
        pd.setIndeterminate(true);
        pd.setCancelable(false);
    }    

}

Any ideas on how to fix this?


The implementation of AsyncTask in one of the other answers is flawed. The progress dialog is being created every time within publishProgress, and the reference to the dialog is not visible outside the method. Here is my attempt:

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new LongOperation().execute();
    }
    class LongOperation extends AsyncTask<String, Void, String> {
        ProgressDialog pd = null;
        TextView tv = null;

        @Override
        protected void onPreExecute(){
            tv = Main.this.findViewById(R.id.textvewid);
            pd = new ProgressDialog(Main.this);
            pd.setMessage("Working...");
            // setup rest of progress dialog
        }
        @Override
        protected String doInBackground(String... params) {
            //perform existing background task
            return result;
        }
        @Override
        protected void onPostExecute(String result){
            pd.dismiss();
            tv.setText(result);
        }
    }
}