不能从静态上下文中引用

Cannot be referenced from a static context

我有以下问题,我试图从 MainActivity.java 调用 InterstitialLaunch.java 以显示插页式广告,但是,在我的情况下,它报告来自 MainActivity.java 的错误并说 'inter_launched (android.content.Context)' 无法从静态上下文 中引用。 这个问题可以做些什么,如何解决这个问题,一些想法,谢谢。

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);

在 MainActivity.java 中显示此错误:

Non-static method 'inter_launched(android.content.Context)' cannot be referenced from a static context

为了从 class 外部调用 inter_launched(Context) 函数,您首先需要实例化 class。

这不是一个很好的例子,因为创建一个 activity 的实例,然后从另一个 class 调用一个函数不是一个好习惯。

为了使您的代码正常工作,您只需重写 InterstitialLaunch Activity 中的 onCreate 函数,然后在其中调用 inter_launched(Context)

现在要创建此 activity,您只需创建一个意图并启动 activity。这是一个例子:

Intent intent = new Intent(context, InterstitialLaunch.class)
startActivity(intent)

让我知道它是否适合你!

在Java中,类似ClassName.method()的意思是调用静态方法。

您的 inter_launched 不是静态方法而是动态(实例)方法,因为该方法没有 static 修饰符。

如果要调用实例方法,则必须执行类似 instanceOfTheClass.method().

的操作

例如:

InterstitialLaunch interstitialLaunch = new InterstitialLaunch();
interstitialLaunch.inter_launched(MainActivity.this);