2013-03-16 144 views
0

我正在尝试将adobot广告添加到我的应用中,但在启动时我在logcat中收到此错误。在我添加admob之前,该应用运行良好,所以我认为这与此有关。Android 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 

的Java的MainMenu的:

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; 


    } 

} 
} 

activity_main_menu 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> 

预先感谢试图帮助。

+2

什么是MainMenu.java:69? – 2013-03-16 19:15:06

+0

mainMenu.setOnClickListener(this);日食不会显示任何错误。 – 2013-03-16 19:17:50

+0

@ user1854482 **当您的代码编译但运行不正常时,会出现'RuntimeExceptions' **。编译成Eclipse的代码没有发出任何警告。 – 2013-03-16 19:20:31

回答

0

我想你在这里没有设置正确的布局。

setContentView(R.layout.activity_main_menu); <<You set the menu layout. 

您必须设置res文件夹中的布局。

如果你有layout.xml在res文件夹然后将它像

setContentView(R.layout.layout); 

编辑:清洁项目并运行它。

+0

我为activity_main_menu添加了xml,因此您可以在res文件夹中看到任何问题 – 2013-03-16 19:24:08

+0

@ user1854482是的,我已经看到清理项目然后运行它。 – 2013-03-16 19:24:28

+0

@ user1854482您是否尝试过并清理过该项目? – 2013-03-16 19:38:44