Reputation: 21
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
Upvotes: 0
Views: 1854
Reputation: 21
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!
Upvotes: 1
Reputation: 7364
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?
Upvotes: 0