添加admob横幅后,我的应用在启动时崩溃

时间:2019-06-06 00:48:14

标签: android firebase admob banner

很抱歉打扰你们,但我是新手,我的应用程序可以正常运行,没有admob广告,但是当我向应用程序添加广告时,它根本无法启动。当我删除这段代码“ mAdView.loadAd(adRequest);”时它启动时没有广告。 任何帮助都很重要

-这是我的日志

06-06 00:57:18.195 3890-3890/com.mhd.ProverbsSimplyExpained E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mhd.ProverbsSimplyExpained, PID: 3890
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mhd.ProverbsSimplyExpained/com.mhd.ProverbsSimplyExpained.SplashActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5019)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at com.mhd.ProverbsSimplyExpained.SplashActivity.onCreate(SplashActivity.java:34)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243) 
    at android.app.ActivityThread.access$800(ActivityThread.java:135) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5019) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
    at dalvik.system.NativeStart.main(Native Method) 

-这是我的Java代码:

package com.mhd.ProverbsSimplyExpained;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.mhd.ProverbsSimplyExpained.db.DataHeper;
import com.mhd.ProverbsSimplyExpained.db.entity.QOD;
import com.mhd.ProverbsSimplyExpained.db.entity.Quote;
import com.mhd.ProverbsSimplyExpained.utils.Constants;
import com.mhd.ProverbsSimplyExpained.utils.CustomSharePreferences;

import java.util.Date;

public class SplashActivity extends Activity {
    DataHeper dataHeper;
    private ImageView imgLoading;
    private AdView mAdView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        // Recherchez Adview comme Resource et chargez une demande.
        // ChangeActivity();
        dataHeper = new DataHeper(getApplicationContext());
        new ChangeActivity().execute();
    }

    private void ChangeActivity() {
        // Create an object of type SplashHandler
        SplashHandler mHandler = new SplashHandler();

        // Create a Message object
        Message msg = new Message();

        // Assign a unique code to the message.
        // Later, this code will be used to identify the message in Handler
        // class.
        msg.what = 0;

        // Send the message with a delay of 3 seconds(3000 = 3 sec).
        mHandler.sendMessageDelayed(msg, 3000);
    }

    private class ChangeActivity extends AsyncTask<Object, Object, Object> {


        @Override
        protected Object doInBackground(Object... params) {
            // TODO Auto-generated method stub
            CustomSharePreferences s = new CustomSharePreferences(
                    getApplicationContext());
            QOD qod = dataHeper.getQOD();
            Date date = new Date();
            int day = 1000 * 60 * 60 * 24;
            if ((date.getTime() - qod.getTime()) >= day) {
                String sTotal = s.getPreferencesString(Constants.TotalQuote);
                if (sTotal.equals("") || sTotal == null) {
                    sTotal = "0";
                }
                int total = Integer.parseInt(sTotal);
                if (total == 0) {
                    total = dataHeper.getTotalQuotesNoFilter();
                    s.setSharePreferencesString(Constants.TotalQuote,
                            String.valueOf(total));
                }

                Quote quote = dataHeper.getQuoteRandom(total);

                dataHeper.SaveQuoteOfDay(quote.getId(), quote.getBody());
            }

            try {
                new Thread();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }


        @Override
        protected void onPostExecute(Object result) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setClass(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            // finish the current activity
            SplashActivity.this.finish();
            super.onPostExecute(result);
        }

    }

    // Handler class implementation to handle the message

    /**
     * The Class SplashHandler.
     */
    private class SplashHandler extends Handler {

        // This method is used to handle received messages
        /*
         * (non-Javadoc)
         *
         * @see android.os.Handler#handleMessage(android.os.Message)
         */
        @Override
        public void handleMessage(Message msg) {
            // switch to identify the message by its code
            switch (msg.what) {
                default:
                case 0:
                    super.handleMessage(msg);

                    // Create an intent to start the new activity.
                    // Our intention is to start MainActivity
                    Intent intent = new Intent();
                    intent.setClass(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                    // finish the current activity
                    SplashActivity.this.finish();
            }
        }
    }

}

-这是我的清单:

<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.mhd.ProverbsSimplyExpained"
android:versionCode="2"
android:versionName="1.5" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="com.app.appname.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<application
    tools:ignore="GoogleAppIndexingWarning"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.CustomDark" >

    <activity
        android:name="com.mhd.ProverbsSimplyExpained.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name="com.mhd.ProverbsSimplyExpained.Widget"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_generic" />
    </receiver>

    <service
        android:name="com.mhd.ProverbsSimplyExpained.AlarmService_Service"
        android:process=":remote" />

    <activity
        android:name="com.mhd.ProverbsSimplyExpained.MainActivity"
        android:screenOrientation="portrait" >
    </activity>

    <activity
        android:name="com.mhd.ProverbsSimplyExpained.activity.QuoteView"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="com.mhd.ProverbsSimplyExpained.activity.QuoteList"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="com.mhd.ProverbsSimplyExpained.activity.FavouristList"
        android:screenOrientation="portrait" >
    </activity>

    <activity
        android:name="com.mhd.ProverbsSimplyExpained.activity.SearchActivity"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="com.mhd.ProverbsSimplyExpained.activity.HelpActivity"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="com.mhd.ProverbsSimplyExpained.activity.SettingActivity"
        android:screenOrientation="portrait" >
    </activity>

    <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-1388495398402044~1611716234"/>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

0 个答案:

没有答案