0

Basically i want to call a method from another class. I want the data() method from DownloadXML.java to be called in MainActivity.java

It is an android application

so here is the MainActivity.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();

    }

}

Here is the DownloadXML.java

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();
    }

}

When i tried to instantiate it and call the method it has an error of:

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

Can you guys help me with this. Thanks

Sudhir Mishra
  • 578
  • 2
  • 16
dotamon
  • 63
  • 3
  • 8

3 Answers3

3

The only code that can go outside a method is variable declarations and initializations. If you want to call a method you have to do it inside a method or constructor.

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) {
        //...
    }

Please try something like this:

    public class MainActivity extends ListActivity {

            String item; 

           @Override
            protected void onCreate(Bundle savedInstanceState) {
                //...
                DownloadXML a = new DownloadXML();
                a.data(); 
            }
}
chrylis -cautiouslyoptimistic-
  • 67,584
  • 19
  • 106
  • 140
ObAt
  • 2,177
  • 3
  • 22
  • 42
  • Your method throws an exception. Probably because the file path does not exists on an Android device. This is another issue and is not related to your first problem. – ObAt Apr 01 '14 at 12:38
  • You need to add a try and catch around `a.data()` – Clad Clad Apr 01 '14 at 12:41
1

Try this:

 public class DownloadXML {

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

and call it like this:

 DownloadXML.data();
Piscean
  • 3,010
  • 12
  • 43
  • 88
1

In order to call a class of another package you will need to relatively code into each package Intents. This thread describes the overall task of how that is done. Essentially one package must be willing to accept whatever the other package flags for it. You should also look up Interprocess Communication. The reason for this is because Android uses Java's "Sandbox" concept. I hope I understood your problem correctly...

Community
  • 1
  • 1
motoku
  • 1,545
  • 1
  • 19
  • 47