[英]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.