该方法findViewById(INT)是未定义 [英] The method findViewById(int) is undefined

查看:151
本文介绍了该方法findViewById(INT)是未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid开发,我想codeA的小应用程序,让我抓住外部JSON文件并解析它。我得到这个工作,但它不会工作,如果我尝试在后台为的AsyncTask 执行。 Eclipse中给我的错误

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

该方法findViewById(INT)是未定义的类型LongOperation

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

在这一行:

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

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

下面是我的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?

推荐答案

的AsyncTask 在其他的答案中的一个的实现是有缺陷的。正在创建进度对话框中的 publishProgress 每一次,而提及的对话框不是法外可见。这里是我的尝试:

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

这篇关于该方法findViewById(INT)是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆