我的应用程序在Droid上的2.2操作系统上运行良好,但是许多用户下载了我的应用程序并收到错误。所有崩溃报告错误均显示如下:

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


这是我的代码:

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.ImageButton06);
        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
}

最佳答案

我的猜测是playernn变量之一是null。几乎可以肯定,您尝试创建太多MediaPlayer对象。这些资源是有限的,您仅应在需要时分配一个,在不需要时立即分配它。由于您可能在单击图像之前才使用播放器,因此请在单击响应方法中创建MediaPlayer,然后在播放声音后将其释放。

关于java - Main.onCreate()中的java.lang.NullPointerException吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4988460/

10-10 09:35