如何从另一个类运行方法? java - android

时间:2014-04-01 12:28:28

标签: java android eclipse

基本上我想从另一个类调用一个方法。我希望在data()

中调用来自DownloadXML.java的{​​{1}}方法

这是一个Android应用程序

所以这是MainActivity.java

MainActivity.java

这是DownloadXML.java

public class MainActivity extends ListActivity {

    String item;       

    DownloadXML a = new DownloadXML();
    a.data(); 
    // SYNTAX ERROR ON TOKEN "DATA" IDENTIFIER EXPECTED AFTER THIS TOKEN

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try{
            item = getItemFromXML(this);
        }catch (XmlPullParserException e){
        }catch (IOException e){
        }

        String[] items = item.split("\n");
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
    }

    public String getItemFromXML(Activity activity) throws XmlPullParserException, IOException{
        StringBuffer stringBuffer = new StringBuffer();
        Resources res = activity.getResources();
        XmlResourceParser xpp = res.getXml(R.xml.items);
        xpp.next();
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT){
            if (eventType == XmlPullParser.START_TAG){
                if (xpp.getName().equals("Item")){

                    stringBuffer.append(xpp.getAttributeValue(null, "Event") + "\n");
                }
                if (xpp.getName().equals("ab")){

                    stringBuffer.append(xpp.getAttributeValue(null, "when") + "\n");
                }
                if (xpp.getName().equals("cd")){

                    stringBuffer.append(xpp.getAttributeValue(null, "where") + "\n" + "----------------------------------------------" + "\n");
                }



            }
            eventType = xpp.next();
        }
        return stringBuffer.toString();

    }

}

当我尝试实例化它并调用该方法时,它有一个错误:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;

public class DownloadXML {

    DownloadXML(){

    }

    public void data() throws Exception{
        URL url = new URL("http://localhost:8080/lab/lab.xml");
          BufferedReader reader = new BufferedReader
          (new InputStreamReader(url.openStream()));
          BufferedWriter writer = new BufferedWriter
          (new FileWriter("C:\\Users\\eyas\\workspace\\CebuHQ\\res\\xml\\items.xml"));
          String line;
          while ((line = reader.readLine()) != null) {
             System.out.println(line);
             writer.write(line);
             writer.newLine();
          }
          reader.close();
          writer.close();
    }

}
你能帮助我吗?感谢

3 个答案:

答案 0 :(得分:3)

唯一可以在方法之外的代码是变量声明和初始化。如果要调用方法,则必须在方法或构造函数中执行此操作。

public class MainActivity extends ListActivity {

    String item;

    DownloadXML a = new DownloadXML();
    a.data(); //This won't work. Put it inside a method

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        //...
    }

请尝试这样的事情:

    public class MainActivity extends ListActivity {

            String item; 

           @Override
            protected void onCreate(Bundle savedInstanceState) {
                //...
                DownloadXML a = new DownloadXML();
                a.data(); 
            }
}

答案 1 :(得分:1)

试试这个:

 public class DownloadXML {

     public static void data(){
        // Do whatever you want
     }
 }

并将其称为:

 DownloadXML.data();

答案 2 :(得分:1)

为了调用另一个包的类,您需要相对编码到每个包Intent中。 This thread描述了如何完成的总体任务。基本上一个包必须愿意接受任何其他包标志。您还应该查找Interprocess Communication。这是因为Android使用Java的“Sandbox”概念。我希望我能正确理解你的问题......