由于Admob广告的实施,活动无法启动

时间:2018-04-17 10:36:22

标签: java android admob

当应用程序在实施AdMob时崩溃时,我一直在努力制作一个包含广告的消息应用程序。不知道为什么应用程序崩溃但我怀疑可能与其他现有代码发生冲突。下面的代码有什么问题吗?

爪哇:

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
    // Sample AdMob app ID: ca-app-pub-5289059028582551~3029249239
       MobileAds.initialize(this, "ca-app-pub-5289059028582551~3029249239");
       AdView adView = (AdView)findViewById(R.id.adView);
       AdRequest adRequest = new AdRequest.Builder().build();
       adView.loadAd(adRequest);
       editMessage = findViewById(R.id.editMessageE);
       mDatabase = FirebaseDatabase.getInstance().getReference().child("Messages");
       mMessageList = findViewById(R.id.messageRec);
       mMessageList.setHasFixedSize(true);
       LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
       linearLayoutManager.setStackFromEnd(true);
       mMessageList.setLayoutManager(linearLayoutManager);
       mAuth = FirebaseAuth.getInstance();
       mAuthListener = new FirebaseAuth.AuthStateListener() {
           @Override
           public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
               if (firebaseAuth.getCurrentUser() == null){
                startActivity(new Intent(MainActivity.this,RegisterActivity.class));
          }
        }
    };
}

Log cat:

  

致命的例外:主要                                                                                                             流程:comdanishansogning.wixsite.httpshobojuniors.danishansgning,PID:3879                                                                                                             java.lang.RuntimeException:无法启动活动ComponentInfo {comdanishansogning.wixsite.httpshobojuniors.danishansgning / comdanishansogning.wixsite.httpshobojuniors.danishansgning.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void com.google.android。关于空对象引用的gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)'

清单:

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
      <activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingStart="16dp"
android:paddingRight="16dp"
android:paddingEnd="16dp"
android:paddingTop="16dp"
android:id="@+id/activity_register"
tools:context="comdanishansogning.wixsite.httpshobojuniors.danishansgning.RegisterActivity"
>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sign In:"
    />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/editEmail"
    android:hint="Enter email"
    android:inputType="textEmailAddress"
    />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/editUsername"
    android:hint="Enter username"
    />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editPassword"
    android:hint="Enter password"
    android:inputType="textPassword"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/signupButtonClicked"
    android:text="Sign up"
    android:onClick="signupButtonClicked"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Already have an Account? Login"
    android:onClick="loginButtonClicked"
    />

<Space
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="ca-app-pub-5289059028582551/8546061599"
    >
</com.google.android.gms.ads.AdView>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

如评论中所述,您正在虚增错误的XML布局文件。在您的代码中:

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

您正在对文件res/layout/activity_main.xml进行充气。要改为res/layout/activity_register.xml,请将setContentView更改为:

setContentView(R.layout.activity_register);

对于第二个问题,您的布局中没有定义RecyclerView。添加一个,例如

<android.support.v7.widget.RecyclerView
    android:id="@+id/messageRec"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

答案 1 :(得分:0)

Please Check weather you have define activity in your manifest file or not.
<application>
<activity android:name=".MainActivity" android:screenOrientation="portrait">
<intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>

否则请检查: - &gt; https://developers.google.com/admob/android/quick-start