简单的Android程序不起作用

时间:2015-04-06 18:40:49

标签: android

Hey Thanx检查我的问题。我刚接触到android编程。我创建了一个启动代码,但它没有在模拟器上启动。它给应用程序意外停止了。请看看它。即时通讯使用Android工作室。

    package com.example.harshit.myapplication;

    import android.app.Activity;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.Bundle;

    public class Splash extends Activity {
    MediaPlayer sound = MediaPlayer.create(Splash.this, R.raw.us);

     @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    sound.start();
    Thread t1 = new Thread() {
        public void run() {
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                Intent Menu = new Intent("com.example.harshit.myapplication.MENU");
                    startActivity(Menu);}
            }


        };
    t1.start();
}

@Override
protected void onPause() {
    super.onPause();
    sound.release();
    finish();
}
}

2 个答案:

答案 0 :(得分:1)

MediaPlayer sound = MediaPlayer.create(Splash.this, R.raw.us);

嘿,你应该把这一行放在oncreate函数中。你没有提供logcat所以错误将是由于这个或你可以检查你的Android清单。你在android清单中声明你的启动类的天气。你必须声明android manifest中的所有类。

答案 1 :(得分:0)

1)您是否在AndroidManifest.xml中包含了您的活动

2) 这里有正确的代码:

      public class splash extends Activity {
MediaPlayer ourSound;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    ourSound=MediaPlayer.create(splash.this,R.raw.splashsound);
    SharedPreferences getprefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean music =getprefs.getBoolean("splashmusic", true);
    if(music == true)
    ourSound.start();
    Thread timer = new Thread()
{
    public void run()
    {
    try
    { sleep(2000);
    }catch(InterruptedException e)
    {e.printStackTrace();
    }
    finally
    {   Intent openMainActivity=new Intent("your package");
        startActivity(openMainActivity);
    }
    }
};
timer.start();

    }

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    ourSound.release();
    finish();
}

}