0

How to call a method from MainActivity? The called method is static in another class. This code works perfectly fine on PC but fails on Android.

Here is the MainActivity code:

public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } Metode.getDatum(); } 

The other public class Metode, has a static method getDatum().

public static String getDatum() { Calendar koledar = Calendar.getInstance(); int[] datum = new int[3]; datum[0] = koledar.get(Calendar.DAY_OF_MONTH); datum[1] = koledar.get(Calendar.MONTH); datum[2] = koledar.get(Calendar.YEAR); String datumString = Arrays.toString(datum); return datumString; } 

I get "Identifier expected" after this token error.

This app has only one activity, the Metode class is

public class Metode 
3
  • I don't understand your problem, where you try call getDatum() method? can you provide some code for this? Commented Aug 17, 2014 at 13:54
  • @Stanislav He is calling the method all the way on the bottom of the main activity, outside any method (which of course is iinvalid syntax). Commented Aug 17, 2014 at 13:58
  • @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; Metode.getDatum(); } This doesn't work either. (Calling from other method). <classname> cannot be resolved. Don't hate me I make a good pie. Commented Aug 17, 2014 at 21:20

2 Answers 2

1

Well, it looks like there was a problem in package declaration.

****package com.example.sluzba;**** import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import java.util.Calendar; import java.util.Scanner; public class Metode { public static String getDatum() { Calendar koledar = Calendar.getInstance(); int[] datum = new int[3]; datum[0] = koledar.get(Calendar.DAY_OF_MONTH); datum[1] = koledar.get(Calendar.MONTH); datum[2] = koledar.get(Calendar.YEAR); String datumString = Arrays.toString(datum); return datumString; 

}

Now it runs, if I call it from other method.

So I guess, package declaration (correct me if naming isn't correct) is important. Just saying, if a n00b like me gets into the same lamery. Thank you, everyone!!! Kao and others. Cheers!

Sign up to request clarification or add additional context in comments.

Comments

0

This code isn't supposed to work. You have to call Metode.getDatum(); from other method.

Moreover, this method returns a String which is totally ignored. Probably you want use this somwhere?

11 Comments

Hmm, ok. I'll repeat this in my head a couple of times, create a method and come back if it works. Thank you for now.
I've got a feeling, that you don't quite understand what are you doing. Am I right?
This returned String is later used to write aquired date to a .txt file. And code works outside Android enviroment. I put the call inside a void method, and now I get "Metode cannot be resolved" and "Identifier expected after.." error again. I think I know I want to call a static method which is declared in other class.
No, calling the method in such a place is incorrect syntax, this has nothing to do with Android. It would be just as wrong in normal Java.
Oh, in Java it is called from the main method..Thanks, guys. But, still. Any suggestions? I do my work around books, but in the meantime, I like to code something.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.