java.lang.NullPointerException in Main.onCreate()?
My app runs fine in 2.2 OS on Droid, but many users downloaded my app and received errors. All the crash report errors show the below:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simpoware.omgannoyingsounds/com.simpoware.omgannoyingsounds.TabBar}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simpoware.omgannoyingsounds/com.simpoware.omgannoyingsounds.Main}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
at android.app.ActivityThread.access$2500(ActivityThread.java:129)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simpoware.omgannoyingsounds/com.simpoware.omgannoyingsounds.Main}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2577)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
at android.widget.TabHost.setCurrentTab(TabHost.java:323)
at android.widget.TabHost.addTab(TabHost.java:213)
at com.simpoware.omgannoyingsounds.TabBar.onCreate(TabBar.java:39)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
... 11 more
Caused by: java.lang.NullPointerException
at com.simpoware.omgannoyingsounds.Main.onCreate(Main.java:58)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
... 20 more
Here is my code:
package com.simpoware.omgannoyingsounds;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import com.greystripe.android.sdk.*;
public class Main extends Activity implements OnClickListener {
// Greystripe SDK object
//private GSSDK sdk;
private BannerView myBanner;
private static final String TAG = "AudioDemo";
private static final String isPlaying = "Media is Playing";
private static final String notPlaying = "Media has stopped Playing";
MediaPlayer player01,player02,player03,player04,player05,player06,player07,player08,player09,player10,player11,player12;
ImageButton imgButton01=new ImageButton (this);
ImageButton imgButton02=new ImageButton (this);
ImageButton imgButton03=new ImageButton (this);
ImageButton imgButton04=new ImageButton (this);
ImageButton imgButton05=new ImageButton (this);
ImageButton imgButton06=new ImageButton (this);
ImageButton imgButton07=new ImageButton (this);
ImageButton imgButton08=new ImageButton (this);
ImageButton imgButton09=new ImageButton (this);
ImageButton imgButton10=new ImageButton (this);
ImageButton imgButton11=new ImageButton (this);
ImageButton imgButton12=new ImageButton (this);
//imgButton02,imgButton03,imgButton04,imgButton05,imgButton06,imgButton07,imgButton08,imgButton09,imgButton10,imgButton11,imgButton12;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//greystripe
//sdk = GSSDK.initialize(this.getApplicationContext(), "ce8dec9e-25e6-45aa-b293-7472e8ba74a0");
myBanner = (BannerView) findViewById(R.id.gsBanner);
myBanner.refresh();
//set audio player
player01 = MediaPlayer.create(this, R.raw.modem);
player02 = MediaPlayer.create(this, R.raw.high_pitch_dog_whistle);
player03 = MediaPlayer.create(this, R.raw.alarm_clock);
player04 = MediaPlayer.create(this, R.raw.faulty_mechanics_sound);
player05 = MediaPlayer.create(this, R.raw.mosquito);
player06 = MediaPlayer.create(this, R.raw.buzz);
player07 = MediaPlayer.create(this, R.raw.strange_static);
player08 = MediaPlayer.create(this, R.raw.piece_of_crap);
player09 = MediaPlayer.create(this, R.raw.house_fire_alarm);
player10 = MediaPlayer.create(this, R.raw.hailstorm);
player11 = MediaPlayer.create(this, R.raw.electric_toothbrush);
player12 = MediaPlayer.create(this, R.raw.ecg);
player01.setLooping(true); // Set looping
player02.setLooping(true); // Set looping
player03.setLooping(true); // Set looping
player04.setLooping(true); // Set looping
player05.setLooping(true); // Set looping
player06.setLooping(true); // Set looping
player07.setLooping(true); // Set looping
player08.setLooping(true); // Set looping
player09.setLooping(true); // Set looping
player10.setLooping(true); // Set looping
player11.setLooping(true); // Set looping
player12.setLooping(true); // Set looping
// Get the ImageButton from XML
imgButton01 = (ImageButton) this.findViewById(R.id.ImageButton01);
imgButton02 = (ImageButton) this.findViewById(R.id.ImageButton02);
imgButton03 = (ImageButton) this.findViewById(R.id.ImageButton03);
imgButton04 = (ImageButton) this.findViewById(R.id.ImageButton04);
imgButton05 = (ImageButton) this.findViewById(R.id.ImageButton05);
imgButton06 = (ImageButton) this.findViewById(R.id.Image开发者_开发技巧Button06);
imgButton07 = (ImageButton) this.findViewById(R.id.ImageButton07);
imgButton08 = (ImageButton) this.findViewById(R.id.ImageButton08);
imgButton09 = (ImageButton) this.findViewById(R.id.ImageButton09);
imgButton10 = (ImageButton) this.findViewById(R.id.ImageButton10);
imgButton11 = (ImageButton) this.findViewById(R.id.ImageButton11);
imgButton12 = (ImageButton) this.findViewById(R.id.ImageButton12);
//Set click listener
imgButton01.setOnClickListener(this);
imgButton02.setOnClickListener(this);
imgButton03.setOnClickListener(this);
imgButton04.setOnClickListener(this);
imgButton05.setOnClickListener(this);
imgButton06.setOnClickListener(this);
imgButton07.setOnClickListener(this);
imgButton08.setOnClickListener(this);
imgButton09.setOnClickListener(this);
imgButton10.setOnClickListener(this);
imgButton11.setOnClickListener(this);
imgButton12.setOnClickListener(this);
}
//more coding...but I only include the necessary coding
}
My guess is that one of the playernn
variables is null
. You are almost certainly trying to create too many MediaPlayer objects. These are a limited resource and you should only allocate one when you need it and release()
it as soon as you do not need it. Since you are probably not using a player until you click on an image, create the MediaPlayer in your click response method, and release it after the sound has played.
If a TabHost is involved, you should use TabHost.setup() between findViewById() and adding tabs to it. Solved my problem.
精彩评论