0

我在第一个项目中正确实施了 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);}

我不知道为什么在我的第二个项目中会发生此错误。任何人都可以帮助我吗?

4

1 回答 1

0

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

     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
于 2021-01-14T03:48:01.527 回答