omerjerk omerjerk - 6 months ago 20
Android Question

how to use a function of another package correctly - android

I've a main package named

omer.ludlowcastle
and another package
omer.ludlowcastle.utils


I wrote this function in
omer.ludlowcastle.utils
:

public boolean checkInternet (){
final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
return true;
} else {
Toast.makeText(getApplicationContext(), "You are not connected to Internet", Toast.LENGTH_LONG).show();
return false;
}
}


and I use this function in an activity in the main package as :

public void Login (View v){
if(omer.ludlowcastle.utils.functions.checkInternet()){
//do other stuff
}

else {
//do other stuff
}
}


but the line in the braces of
if
gives the following error :

Cannot make a static reference to the non-static method checkInternet() from the type functions


How to solve this ?

Answer

You are getting Cannot make a static reference to the non-static method checkInternet() from the type functions because method is not static. You have 2 options:

  1. Either declare the class Functions and method checkInternet() as static. It is more appropriate way as it is method of Utility class. So you can call it as you are using it right now.

2.Or create an object of class as

Functions funObj = new Functions();

And then call method using object as

public void Login (View v){
    if(funObj.checkInternet()){
        //do other stuff
    }

    else {
        //do other stuff
    }
}

Hope it helps.

Comments