[英]How to access a variable from another class? android java
花了几个小时试图弄清楚为什么“下载器”类无法读取“fetch”方法中的“ finalURL ”变量? 真的很感激任何指针..
这是一个词汇改进应用程序,因此它从字典 api 中获取 XML 数据。
public class DefineWord extends Activity {
static final String head = new String("http://www.dictionaryapi.com/api/v1/references/collegiate/xml/");
static final String apikey = new String("?key=xxxxxx-xxx-xxx-xxxx-xxxxx");
TextView src;
EditText searchBar;
在创建:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.defineword);
src=(TextView)findViewById(R.id.textFromWeb);
searchBar = (EditText)findViewById(R.id.searchBar);
}
取法:
public void fetch(View v){ //onClick in xml starts this "fetch" method
String w = searchBar.getText().toString(); // get the text the user enters into searchbar
String finalURL = head.trim() + w.trim() + apikey.trim(); //concatenate api url
Downloader d = new Downloader();
d.execute(finalURL); // Calls AsyncTask to connect in the background.
}
异步任务:
class Downloader extends AsyncTask<String,Void,String>{
ArrayList<String> information = new ArrayList<String>(); // Store fetched XML data in ArrayList
public String doInBackground(String... urls) {
String result = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
问题就在这里。 “ finalURL ”无法解析为变量。
Document doc = builder.parse(finalURL); //finalURL cannot be resolved to a variable ???
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("entry"); // Make a list of all elements in XML file with tag name "entry"
for (int temp = 0; temp < nList.getLength(); temp++) { //Iterate over elements and get their attributes (Definition, etymology...)
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE){
Element eElement = (Element) nNode;
String element = ("\nCurrent Element : " + eElement.getAttribute("id"));
information.add(element); //
String definitions = ("\nDefinition : \n" + eElement.getElementsByTagName("dt").item(0).getTextContent());
information.add(definitions);
}
}
}catch (ParserConfigurationException e){
e.printStackTrace();
}catch(SAXException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return result;
}
protected void onPostExecute (String result){
String finalresult = information.toString();
src.setText(finalresult);
}
}
}
谢谢你的时间。
这称为参数。 通过使用asyncTask.execute(parameter)
将对象传递给doInBackground(String... arrayOfParameters)
。
所以要再次访问该值,您应该使用builder.parse(urls[0]);
finalURL 在 fetch 方法中声明,因此只能在此函数中访问。
如果您希望它可以在整个程序中访问,请像这样在函数之外声明它。
class MyClass{
public static String finalURL; // variable declaration
public void fetch(View v){
finalURL = head.trim() + w.trim() + apikey.trim(); // variable assignment
}
public void otherFunction(){
Document doc = builder.parse(finalURL); // retrieves static class variable
}
}
我希望这回答了你的问题。 如果没有,让我知道混乱是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.