我正在尝试向我的应用程序添加 admob 广告,但是当它启动时我在 logcat 中收到此错误。在我添加 admob 之前,该应用程序运行良好,所以我认为这与此有关。
03-16 19:08:00.683: E/AndroidRuntime(529): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.musicbynumbers.scalesads/com.musicbynumbers.scalesads.MainMenu}: java.lang.NullPointerException
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.os.Looper.loop(Looper.java:123)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-16 19:08:00.683: E/AndroidRuntime(529): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 19:08:00.683: E/AndroidRuntime(529): at java.lang.reflect.Method.invoke(Method.java:521)
03-16 19:08:00.683: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-16 19:08:00.683: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-16 19:08:00.683: E/AndroidRuntime(529): at dalvik.system.NativeStart.main(Native Method)
03-16 19:08:00.683: E/AndroidRuntime(529): Caused by: java.lang.NullPointerException
03-16 19:08:00.683: E/AndroidRuntime(529): at com.musicbynumbers.scalesads.MainMenu.onCreate(MainMenu.java:69)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-16 19:08:00.683: E/AndroidRuntime(529): ... 11 more
MainMenu的Java:
package com.musicbynumbers.scales;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import com.musicbynumbers.pianoscalesadscopy.R;
public class MainMenu extends LicenseCheckActivity implements View.OnClickListener {
Button majScales, minHarm, minMel, majArp, minArp, chrome, cont, pent,how;
ImageButton mainMenu;
Intent j;
Intent k;
Intent l;
Intent m;
Intent n;
Intent o;
Intent p;
Intent q;
Intent r;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
setContentView(R.layout.activity_main_menu);
Toast.makeText(this, "Checking Application License", Toast.LENGTH_SHORT).show();
// Check the license
//checkLicense();
j = new Intent(MainMenu.this, majorScales.class);
k = new Intent(MainMenu.this, minorHarmonic.class);
l = new Intent(MainMenu.this, MajorArpeggios.class);
m = new Intent(MainMenu.this, MinorArpeggios.class);
n = new Intent(MainMenu.this, ChromaticScales.class);
o = new Intent(MainMenu.this, ContraryMotion.class);
p = new Intent(MainMenu.this, PentatonicScales.class);
q = new Intent(MainMenu.this, minorMelodic.class);
r = new Intent(MainMenu.this, HowItWorks.class);
mainMenu = (ImageButton) findViewById(R.id.imagelogo);
majScales = (Button) findViewById(R.id.majorscalesb);
minHarm = (Button) findViewById(R.id.minorharmonicb);
minMel = (Button) findViewById(R.id.minormelodicb);
majArp = (Button) findViewById(R.id.majorarpeggiosb);
minArp = (Button) findViewById(R.id.minorarpeggiosb);
chrome = (Button) findViewById(R.id.chromaticscalesb);
cont = (Button) findViewById(R.id.contraryb);
pent = (Button) findViewById(R.id.pentatonicscaleb);
how = (Button) findViewById(R.id.howitworksb);
majScales.setOnClickListener(this);
mainMenu.setOnClickListener(this);
minHarm.setOnClickListener(this);
minMel.setOnClickListener(this);
majArp.setOnClickListener(this);
minArp.setOnClickListener(this);
chrome.setOnClickListener(this);
cont.setOnClickListener(this);
pent.setOnClickListener(this);
how.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.imagelogo:
Intent i = new Intent(MainMenu.this, MainMenu.class);
startActivity(i);
break;
case R.id.majorscalesb:
startActivity(j);
break;
case R.id.minorharmonicb:
startActivity(k);
break;
case R.id.majorarpeggiosb:
startActivity(l);
break;
case R.id.minorarpeggiosb:
startActivity(m);
break;
case R.id.chromaticscalesb:
startActivity(n);
break;
case R.id.contraryb:
startActivity(o);
break;
case R.id.pentatonicscaleb:
startActivity(p);
break;
case R.id.minormelodicb:
startActivity(q);
break;
case R.id.howitworksb:
startActivity(r);
break;
}
}
}
活动主菜单 xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/musicbynumbersbg"
android:orientation="vertical"
android:weightSum="100"
tools:context=".MainMenu" >
<ImageButton
android:id="@+id/imagelogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="90"
android:background="@drawable/topbanner"
android:gravity="center" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<Button
android:id="@+id/howitworksb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="How to Use the App" />
<Button
android:id="@+id/majorscalesb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Major Scales" />
<Button
android:id="@+id/minormelodicb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Minor Melodic Scales" />
<Button
android:id="@+id/minorharmonicb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Minor Harmonic Scales" />
<Button
android:id="@+id/majorarpeggiosb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Major Arpeggios" />
<Button
android:id="@+id/minorarpeggiosb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Minor Arpeggios" />
<Button
android:id="@+id/chromaticscalesb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Chromatic Scales" />
<Button
android:id="@+id/contraryb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Contrary Motion" />
<Button
android:id="@+id/pentatonicscaleb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Pentatonic Scale" />
</LinearLayout>
</ScrollView>
<com.google.ads.AdView android:id="@+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId=" a15144ab55b99a9"
ads:loadAdOnCreate="true"
ads:adSize="BANNER"
/>
</LinearLayout>
提前感谢您提供帮助。