You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android报Cannot be referenced from a static context错误如何解决

问题原因

你的错误本质是:inter_launched是InterstitialLaunch类的非静态成员方法,必须通过类的实例对象调用,你直接用类名调用属于静态调用场景,自然会报错。另外代码本身设计存在不合理的地方:InterstitialLaunch不需要继承Activity,它只是用来处理插屏广告逻辑的工具类,不需要承担页面生命周期功能。

最优解决方案:改造为普通广告工具类,调整为静态方法

直接修改InterstitialLaunch.java,去掉Activity继承,把核心方法改为静态,同时修正原代码里依赖Activity上下文的问题:

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InitializationStatus;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdapterStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import java.util.Map;

// 移除Activity继承,改为普通工具类
public class InterstitialLaunch {
    // 自定义两个触发阈值常量,可根据需求调整数值
    private static final int LAUNCHES_UNTIL_PROMPT = 3;
    private static final int DAYS_UNTIL_PROMPT = 7;

    // 方法改为静态
    public static void inter_launched(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences("interlaunch", 0);
        SharedPreferences.Editor editor = prefs.edit();

        // 原有启动计数逻辑不变
        long launch_count = prefs.getLong("launch_count", 0) + 1;
        editor.putLong("launch_count", launch_count);

        Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
        if (date_firstLaunch == 0) {
            date_firstLaunch = System.currentTimeMillis();
            editor.putLong("date_firstlaunch", date_firstLaunch);
        }

        if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
            if (System.currentTimeMillis() >= date_firstLaunch +
                    (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000L)) {
                interstitialLaunch(mContext, editor);
            }
        }
        editor.apply();
    }

    // 方法改为静态
    public static void interstitialLaunch(final Context mContext, final SharedPreferences.Editor editor) {
        // 原this替换为传入的mContext
        MobileAds.initialize(mContext, 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()));
                }

                // getString通过mContext调用,广告展示要求传入Activity上下文
                InterstitialAd.load(mContext,
                        mContext.getString(R.string.interstitial_id),
                        new AdRequest.Builder().build(),
                        new InterstitialAdLoadCallback() {
                            @Override
                            public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                                interstitialAd.show((Activity) mContext);
                            }
                        }
                );
            }
        });
    }
}

修改完成后,你原有的MainActivity调用代码InterstitialLaunch.inter_launched(MainActivity.this);即可正常运行。

注意事项
  • 不要手动创建Activity类的实例,Activity实例由系统生命周期管理,自行new出的实例没有有效上下文环境,无法正常调用资源和页面相关方法
  • 调用inter_launched时传入的Context必须是Activity实例,不要使用Application上下文,否则广告无法正常弹出展示

内容的提问来源于stack exchange,提问作者Dejo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 12:39:04