空对象引用上的 startapp 广告错误代码 'boolean android.content.Context.isUiContext()'

startapp ads error code 'boolean android.content.Context.isUiContext()' on a null object reference

我在我的第一个项目中正确实施了 StartApp 广告。一切运行良好。然后在另一个项目中出现一些错误,错误代码如下:

Process: com.dev.project, PID: 16570
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.project/com.dev.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Context.isUiContext()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Context.isUiContext()' on a null object reference

以下是我在 StartApp 文档中实现 StartApp 广告的代码:

public class MainActivity extends AppCompatActivity implements CurrentSessionCallback {
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.activity_main);

        StartAppSDK.init(this, STARTAPPID, true);
        StartAppAd.disableSplash();
        startappshow();

     public void startappshow() {

        Banner startAppBanner = new Banner(context, new BannerListener() {
            @Override
            public void onReceiveAd(View banner) {
                // banner is ready. Add it to your view if needed
            }
            @Override
            public void onFailedToReceiveAd(View banner) {
            }

            @Override
            public void onImpression(View view) {

            }

            @Override
            public void onClick(View banner) {
            }
        });

        // Get the Main relative layout of the entire activity
        RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.banner_container);
        // Define StartApp Banner
        RelativeLayout.LayoutParams bannerParameters =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        bannerParameters.addRule(RelativeLayout.CENTER_HORIZONTAL);
        bannerParameters.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        // Add to main Layout
        mainLayout.addView(startAppBanner, bannerParameters);}

我不知道为什么我的第二个项目会出现这个错误。谁能帮帮我?

您只需在调用 startappshow() 之前初始化您的 context,如下所示

     Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.activity_main);

        StartAppSDK.init(this, STARTAPPID, true);
        StartAppAd.disableSplash();
        context = this; // add this line
        startappshow();

或者您可以像下面这样简单地使用 this

Banner startAppBanner = new Banner(this, new BannerListener(){...} // replace context with this