I have the following problem, I am trying to call InterstitialLaunch.java from MainActivity.java to display Interstitial, however, in my case it reports an error from MainActivity.java and says that 'inter_launched (android.content.Context)' cannot be referenced from a static context. What can be done about this problem and how to solve the problem, some idea, thanks.
InterstitialLaunch.java
public class InterstitialLaunch extends Activity {
public void inter_launched(Context mContext) {
SharedPreferences prefs = mContext.getSharedPreferences("interlaunch", 0);
SharedPreferences.Editor editor = prefs.edit();
// Increment launch counter
long launch_count = prefs.getLong("launch_count", 0) + 1;
editor.putLong("launch_count", launch_count);
// Get date of first launch
Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
if (date_firstLaunch == 0) {
date_firstLaunch = System.currentTimeMillis();
editor.putLong("date_firstlaunch", date_firstLaunch);
}
// Wait at least n days before opening
if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
if (System.currentTimeMillis() >= date_firstLaunch +
(DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
interstitialLaunch(mContext, editor);
}
}
editor.apply();
}
public void interstitialLaunch(final Context mContext, final SharedPreferences.Editor editor) {
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
Map<String, AdapterStatus> statusMap = initializationStatus.getAdapterStatusMap();
for (String adapterClass : statusMap.keySet()) {
AdapterStatus status = statusMap.get(adapterClass);
Log.d("MyApp", String.format(
"Adapter name: %s, Description: %s, Latency: %d",
adapterClass, status.getDescription(), status.getLatency()));
}
InterstitialAd.load(getApplicationContext(),
getString(R.string.interstitial_id),
new AdRequest.Builder().build(),
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
interstitialAd.show(InterstitialLaunch.this);
}
}
);
}
});
}
}
MainActivity.java
InterstitialLaunch.inter_launched(MainActivity.this);
Shows this error in MainActivity.java:
Non-static method 'inter_launched(android.content.Context)' cannot be referenced from a static context
In Java, something like ClassName.method()
means invoking a static method.
Your inter_launched
is not a static but a dynamic (instance) method because the method doesn't have static
modifier.
If you would invoke an instance method, you must do something like instanceOfTheClass.method()
.
For example:
InterstitialLaunch interstitialLaunch = new InterstitialLaunch();
interstitialLaunch.inter_launched(MainActivity.this);