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

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

问题描述

我是 Android 开发的新手,我正在尝试编写一个小应用程序,它允许我获取外部 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

未定义 LongOperation 类型的 findViewById(int) 方法

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

在这一行:

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

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

这是我的代码:

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天全站免登陆