[英]The method findViewById(int) is undefined
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. 我是Android开发的新手,我正在尝试编写一个小应用程序,它允许我获取外部JSON文件并解析它。 I got this to work, however it wont work if I try to execute it in the background as an
AsyncTask
. 我得到了它的工作,但如果我尝试在后台执行它作为
AsyncTask
它不会工作。 Eclipse gives me the error Eclipse给了我错误
The method findViewById(int) is undefined for the type LongOperation
对于LongOperation类型,方法findViewById(int)未定义
in this line: 在这一行:
TextView txtView1 = (TextView)findViewById(R.id.TextView01);
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? 有想法该怎么解决这个吗?
Here is what you should do to make it work as you want. 这是你应该做的,使它按你想要的方式工作。 Use onPostExecude()
使用onPostExecude()
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new LongOperation(this).execute();
}
}
class LongOperation extends AsyncTask<String, Void, String> {
private Main longOperationContext = null;
public LongOperation(Main context) {
longOperationContext = context;
Log.v("LongOper", "Konstuktor");
}
@Override
protected String doInBackground(String... params) {
Log.v("doInBackground", "inside");
StringBuilder sb = new StringBuilder();
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);
Log.v("line = ", "jo.getString() ="+jo.getString("text"));
sb.append(jo.getString("text") + " - " + jo.getString("secondtest")).append("\n");
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.v("Error", "URL exc");
} catch (IOException e) {
e.printStackTrace();
Log.v("ERROR", "IOEXECPTOIn");
} catch (JSONException e) {
e.printStackTrace();
Log.v("Error", "JsonException");
}
String result = sb.toString();
return result;
}
@Override
protected void onPostExecute(String result) {
Log.v("onPostExe", "result = "+result);
TextView txtView1 = (TextView)longOperationContext.findViewById(R.id.textView01);
txtView1.setText(result);
}
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
ProgressDialog pd = new ProgressDialog(longOperationContext);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Working...");
pd.setIndeterminate(true);
pd.setCancelable(false);
}
}
You are trying to do something which won't work. 你正在尝试做一些不起作用的事情。 First of all you are inside of a class that extends
AsyncTask
so you won't have that method available as it is a method of the class Activity
. 首先,您在扩展
AsyncTask
的类中,因此您不会使用该方法,因为它是Activity
类的方法。
The second problem is that you are trying to do UI stuff in a method that is not synchronized with the UI thread. 第二个问题是您正在尝试在与UI线程不同步的方法中执行UI内容。 That is nothing you would want to do.
这不是你想做的事。
Process your JSON response in the doInBackground
method and pass the result to the onPostExecute
method where you will be able to handle UI stuff as it is synchronized with the UI thread. 在
doInBackground
方法中处理您的JSON响应,并将结果传递给onPostExecute
方法,在该方法中,当UI与UI线程同步时,您将能够处理UI内容。
The current setup you have will not make it easier for you to handle what you are trying to do anyway. 您当前的设置将无法让您更轻松地处理您尝试执行的操作。 You could make your
LongOperation
class a private class of your Activity
class and define the TextView
as a instance member. 您可以将
LongOperation
类设置为Activity
类的私有类,并将TextView
定义为实例成员。 Grab it off the layout using findViewById
inside of your OnCreate
and modify (set text or whatever) inside the onPostExecute
method of your AsyncTask
. 使用
OnCreate
findViewById
其从布局中onPostExecute
,并在AsyncTask
的onPostExecute
方法中修改(设置文本或其他内容)。
I hope it is somewhat clear what I meant. 我希望我的意思有点清楚。
The implementation of AsyncTask
in one of the other answers is flawed. 在其中一个答案中实现
AsyncTask
是有缺陷的。 The progress dialog is being created every time within publishProgress
, and the reference to the dialog is not visible outside the method. 每次在
publishProgress
创建进度对话框,并且在方法外部看不到对话框的引用。 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 is method in Activity class. findViewById是Activity类中的方法。 You should pass instance of your activity to your LongOperation when you create it.
您应该在创建活动时将实例的实例传递给LongOperation。 Then use that instance to call findViewById.
然后使用该实例调用findViewById。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.