The method findViewById(int) is undefined

前端 未结 4 918
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 02:08

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

4条回答
  •  迷失自我
    2021-01-03 03:03

    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 {
            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);
            }
        }
    }
    

提交回复
热议问题