Main.onCreate()中的java.lang.NullPointerException?

Main.onCreate()中的java.lang.NullPointerException?,java,android,Java,Android,我的应用程序在Droid上的2.2操作系统中运行良好,但许多用户下载了我的应用程序并收到错误。所有崩溃报告错误如下所示: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simpoware.omgannoyingsounds/com.simpoware.omgannoyingsounds.TabBar}: java.lang.RuntimeException: Unable to start activ

我的应用程序在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,并在声音播放后释放它。

如果涉及TabHost,则应在findViewById()和向其中添加选项卡之间使用TabHost.setup()。解决了我的问题。

这可能不是问题所在,但您正在使用
新建ImageButton(This)
在构造函数中创建新的ImageButton(隐式)。但随后您将它们设置为resource
imgButton01=(ImageButton)this.findViewById(R.id.ImageButton01)您应该执行一项或另一项操作。在任务结束后,尝试将所有对象设为空。由于创建了更多对象,我认为创建更多堆可能会导致此错误。我也同意Falmari.Shashank_Itmaster的观点——我对Java还是个新手。你能给我举几个例子吗?使对象为空意味着显式地释放它们的内存。在我们的例子中,每当您完成与该对象相关的任务时,尝试使用像这样的imgButtton1=null。Shashank_Itmaster的建议是出于善意的,但通常不是必要的。您并没有耗尽内存,而是得到了一个与此完全不同的NullPointerException——通常是在尚未初始化的对象引用上调用方法。通常不需要将变量设置为null。第58行:player08.setLooping(true);//设置循环。起初我认为这可能与操作系统版本有关,但现在,我倾向于手机的硬件规格。你们说得对,我不需要那么多的MediaPlayer。问题是,我想将我的mediaplayer设置为以循环方式播放声音,除非播放器再次按下按钮停止播放。目前,有12种不同的声音可以播放。谢谢,我想我已经得到了解决这个问题所需的信息。我可以确认这个应用程序在droid和evo上运行良好。它轰炸了Droid2、milestone和wildfire。我想知道操作系统版本是否与此有关?我知道我的原始机器人在2.2.1上,我的同事机器人2在2.2上。嗯……我想我会继续前进,做其他事情。