插入广告时按钮点击app crashesh

时间:2018-01-30 06:35:50

标签: android admob interstitial

可以使用横幅广告。但是当加载intertitial广告时,按钮点击后app会崩溃,这会加载广告和调用方法submitpost();广告关闭后。 我用firebase连接了admob。 没有插页式广告,submitpost();单击mSubmitPost按钮后执行方法。

我已经搜索了解决方案,但一无所获。请帮我。 此java代码来自google firebase示例,并且在没有插页式广告的情况下正常运行。

这是我的代码:

package com.dharatirths.yatras.mohim;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;


import java.util.HashMap;
import java.util.Map;

public class NewPost extends BaseActivity {

    private static final String TAG = "NewPostActivity";
    private static final String REQUIRED = "Required";

    private InterstitialAd mInterstitialAd;

    // [START declare_database_ref]
    private DatabaseReference mDatabase;
    // [END declare_database_ref]

    private EditText mTitleField;
    private EditText mBodyField;
    private FloatingActionButton mSubmitButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_post);

        // [START initialize_database_ref]
        mDatabase = FirebaseDatabase.getInstance().getReference();
        // [END initialize_database_ref]

        mTitleField = findViewById(R.id.field_title);
        mBodyField = findViewById(R.id.field_body);
        mSubmitButton = findViewById(R.id.fab_submit_post);
        mSubmitButton.setEnabled(true);

        mSubmitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showIntAdd();
            }
        });

        mInterstitialAd = createNewIntAd();
        loadIntAdd();
    }

    private InterstitialAd createNewIntAd() {
        InterstitialAd intAd = new InterstitialAd(getApplicationContext());
        // set the adUnitId (defined in values/strings.xml)
        intAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        intAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                mSubmitButton.setEnabled(true);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                mSubmitButton.setEnabled(true);
            }

            @Override
            public void onAdClosed() {
                // Proceed to the next level.
               submitPost();
            }
        });
        return intAd;
    }
    private void showIntAdd() {

// Show the ad if it's ready. Otherwise toast and reload the ad.
        if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            submitPost();
        }
    }
    private void loadIntAdd() {
        // Disable the  level two button and load the ad.
        mSubmitButton.setEnabled(false);
        AdRequest adRequest = new AdRequest.Builder().build();
        mInterstitialAd.loadAd(adRequest);
    }


    private void submitPost() {
        final String title = mTitleField.getText().toString();
        final String body = mBodyField.getText().toString();

        // Title is required
        if (TextUtils.isEmpty(title)) {
            mTitleField.setError(REQUIRED);
            return;
        }

        // Body is required
        if (TextUtils.isEmpty(body)) {
            mBodyField.setError(REQUIRED);
            return;
        }

        // Disable button so there are no multi-posts
        setEditingEnabled(false);
        Toast.makeText(this, "Posting...", Toast.LENGTH_SHORT).show();

        // [START single_value_read]
        final String userId = getUid();
        mDatabase.child("users").child(userId).addListenerForSingleValueEvent(
                new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        // Get user value
                        User user = dataSnapshot.getValue(User.class);

                        // [START_EXCLUDE]
                        if (user == null) {
                            // User is null, error out
                            Log.e(TAG, "User " + userId + " is unexpectedly null");
                            Toast.makeText(NewPost.this,
                                    "Error: could not fetch user.",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            // Write new post
                            writeNewPost(userId, user.username, title, body);
                        }

                        // Finish this Activity, back to the stream
                        setEditingEnabled(true);
                        finish();
                        // [END_EXCLUDE]
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.w(TAG, "getUser:onCancelled", databaseError.toException());
                        // [START_EXCLUDE]
                        setEditingEnabled(true);
                        // [END_EXCLUDE]
                    }
                });
        // [END single_value_read]
    }

    private void setEditingEnabled(boolean enabled) {
        mTitleField.setEnabled(enabled);
        mBodyField.setEnabled(enabled);
        if (enabled) {
            mSubmitButton.setVisibility(View.VISIBLE);
        } else {
            mSubmitButton.setVisibility(View.GONE);
        }
    }

    // [START write_fan_out]
    private void writeNewPost(String userId, String username, String title, String body) {
        // Create new post at /user-posts/$userid/$postid and at
        // /posts/$postid simultaneously
        String key = mDatabase.child("posts").push().getKey();
        Post post = new Post(userId, username, title, body);
        Map<String, Object> postValues = post.toMap();

        Map<String, Object> childUpdates = new HashMap<>();
        childUpdates.put("/posts/" + key, postValues);
        childUpdates.put("/user-posts/" + userId + "/" + key, postValues);

        mDatabase.updateChildren(childUpdates);
    }
    // [END write_fan_out]
}

这是logcat

01-30 11:53:20.778 28873-28873/com.dharatirths.yatras.mohim E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.dharatirths.yatras.mohim, PID: 28873
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dharatirths.yatras.mohim/com.google.android.gms.ads.AdActivity}: java.lang.IllegalStateException: Only fullscreen activities can request orientation
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                  at android.os.Looper.loop(Looper.java:164)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                               Caused by: java.lang.IllegalStateException: Only fullscreen activities can request orientation
                                                                                  at android.os.Parcel.readException(Parcel.java:1950)
                                                                                  at android.os.Parcel.readException(Parcel.java:1888)
                                                                                  at android.app.IActivityManager$Stub$Proxy.setRequestedOrientation(IActivityManager.java:5675)
                                                                                  at android.app.Activity.setRequestedOrientation(Activity.java:5739)
                                                                                  at com.google.android.gms.ads.internal.overlay.d.a(:com.google.android.gms.DynamiteModulesA@11975470:182)
                                                                                  at com.google.android.gms.ads.internal.overlay.d.b(:com.google.android.gms.DynamiteModulesA@11975470:216)
                                                                                  at com.google.android.gms.ads.internal.overlay.d.a(:com.google.android.gms.DynamiteModulesA@11975470:76)
                                                                                  at com.google.android.gms.ads.internal.overlay.client.c.onTransact(:com.google.android.gms.DynamiteModulesA@11975470:8)
                                                                                  at android.os.Binder.transact(Binder.java:604)
                                                                                  at com.google.android.gms.internal.zzeu.zzb(Unknown Source:7)
                                                                                  at com.google.android.gms.internal.zzxg.onCreate(Unknown Source:8)
                                                                                  at com.google.android.gms.ads.AdActivity.onCreate(Unknown Source:29)
                                                                                  at android.app.Activity.performCreate(Activity.java:6975)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                                  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                  at android.os.Looper.loop(Looper.java:164) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
01-30 11:53:20.990 28873-28896/com.dharatirths.yatras.mohim D/FA: Logging event (FE): ad_impression(_ai), Bundle[{firebase_event_origin(_o)=am, ad_event_id(_aeid)=1139149630212468820}]

0 个答案:

没有答案