android函数未定义,android – 方法findViewById(int)未定义
我是
Android開發(fā)的新手,我正在嘗試編寫一個(gè)小應(yīng)用程序,它允許我獲取外部JSON文件并解析它.我得到了它的工作,但如果我嘗試在后臺(tái)執(zhí)行它作為AsyncTask它不會(huì)工作. Eclipse給了我錯(cuò)誤
The method findViewById(int) is undefined for the type LongOperation
在這一行:
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 {
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);
}
}
有想法該怎么解決這個(gè)嗎?
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的android函数未定义,android – 方法findViewById(int)未定义的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Visual Studio语言设置
- 下一篇: 无线蓝牙耳机到底该怎么选电脑如何选择蓝牙