简体   繁体   English

当我按下“开始”按钮时,它应该转到下一个活动,但它会转到主屏幕

[英]When I press button Start it should go to next activity but it goes to home screen

When I press button Start It should go to next activity but it goes to home screen.当我按下按钮开始时,它应该转到下一个活动,但它会转到主屏幕。 This problem I faced when I have added banner ad code.我在添加横幅广告代码时遇到的这个问题。

The Button which is named as startbtn in XML code and btnclk1 in Java code.在 XML 代码中名为 startbtn,在 Java 代码中名为 btnclk1 的按钮。 I am doing this project in Android Studio.我在 Android Studio 中做这个项目。

XML Code: XML 代码:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splashscr"
    tools:context=".SplashScreen">

    <Button
        android:id="@+id/startbtn2"
        android:layout_width="85dp"
        android:layout_height="32dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/black"
        android:text="Show Ad"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.625" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="239dp"
        android:layout_height="221dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.287"
        app:srcCompat="@drawable/criclive" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="288dp"
        android:layout_height="20dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.507"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/credits" />

    <Button
        android:id="@+id/startbtn"
        android:layout_width="85dp"
        android:layout_height="32dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/black"
        android:text="Start"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.559" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="325dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-8972808858904277/7753758329"/>

</android.support.constraint.ConstraintLayout>

Java code:爪哇代码:

    package com.airobotfyp.livecricket;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;


public class SplashScreen extends AppCompatActivity {
    private InterstitialAd mInterstitialAd;
    private AdView mAdView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        Button btnclk1 = (Button) findViewById(R.id.startbtn);
        Button btnclk2 = (Button) findViewById(R.id.startbtn2);

        MobileAds.initialize(this, "ca-app-pub-8972808858904277~8136901702");

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pub-8972808858904277/7753758329");
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Code to be executed when an ad request fails.
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
        //full
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-8972808858904277/3431369930");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                // Load the next interstitial.
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }
        });
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Code to be executed when an ad request fails.
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when the ad is displayed.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when when the interstitial ad is closed.
            }
        });

        btnclk1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    Log.d("TAG", "The interstitial wasn't loaded yet.");
                }
                Intent mainIntent = new Intent(SplashScreen.this,Menu.class);
                startActivity(mainIntent);
            }
        });
        btnclk2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    Log.d("TAG", "The interstitial wasn't loaded yet.");
                }
            }
        });

    }
}

逻辑猫 This is where the logcat resides.这是 logcat 所在的位置。 PLease post the error that might be popping up in the logcat here.请在此处发布可能在 logcat 中弹出的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 屏幕关闭时如何开始活动? - How do i start activity when screen goes off? 当我在当前活动中按按钮时如何更改下一个活动背景 - how to change next activity background when i press button in current activity 我应该从哪里开始一个应该在应用程序进入后台时触发的活动? - Where should I start an activity which is suppose to be triggered when app goes to background? 当按下 HOME 按钮时,防止 android 活动到 go 暂停 state - Prevent an android activity to go pause state when HOME button is pressed 当活动失去焦点时(当主屏幕或屏幕关闭时),Media Player会暂停音乐 - Media Player pauses the music when the activity loses focus(when home or screen goes off) 从第二个活动中按下主页返回按钮时不会在活动中返回相同的片段 - From second activity when press home back button do not came back same fragment in a activity 当我按下主屏幕或后退按钮时音乐不会停止 - Music doesn't stop when I press home or back button 为什么当我尝试回家活动时应用程序崩溃 - Why the app is crashing when I trying to go to home activity 在android Studio中,我想要当我单击按钮时,下一个活动/片段应该来自右侧 - In android Studio, I want when i click on button , next activity/fragment should come from right side 从启动屏幕启动活动,我应该使用run()还是runOnUiThread()? - Start an Activity from Splash Screen, should I use run() or runOnUiThread()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM