Luke Batley
Luke Batley

Reputation: 2402

import a class and implement a function in Android

i'm creating an App preferences class that i can put functions in that i use app wide such as a check internet connection function. what i'm trying to do is import the class into my activity the run one its functions in the on create. does anyone know how to do this?

heres what i've got so far

import android.app.Activity;
import android.os.Bundle;
import co.myapp.AppPreferences;

public class Loading extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lo_loading);
        AppPreferences.class.checkInternet()
    }
}

heres my AppPreferences.java

public class AppPreferences {


public void checkInternet(){

    Log.v("Pref", "checking internet");

}   

}

Upvotes: 1

Views: 1777

Answers (2)

jtt
jtt

Reputation: 13541

You have to instantiate an object of type Apppreference in order to access its methods (unless they are static)

Upvotes: 0

user180100
user180100

Reputation:

checkInternet() is non-static you need an instance of AppPreferences in your activity and use the method on this instance:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lo_loading);
    AppPreferences appPrefs = new AppPreferences()
    appPrefs.checkInternet()
}

Another solution is to make checkInternet() static.

Upvotes: 3

Related Questions