应用程序无法在Android 2.3上启动

时间:2013-04-16 10:22:55

标签: java android logcat

我的应用程序不想在Android 2.3上启动,即使我将其设为最低2.2,这里是LogCat。我在模拟器和2.3 Android设备上进行了测试,它根本没有运行,但在我的其他4.0设备上它运行得很好......可能有什么问题?

04-16 13:18:40.982: D/dalvikvm(6191): GC_CONCURRENT freed 41K, 49% free 2761K/5379K, external 3443K/4300K, paused 3ms+42ms
04-16 13:18:41.652: D/dalvikvm(6191): GC_CONCURRENT freed 1K, 49% free 2761K/5379K, external 4643K/4643K, paused 2ms+10ms
04-16 13:18:41.942: D/AndroidRuntime(6191): Shutting down VM
04-16 13:18:41.942: W/dalvikvm(6191): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
04-16 13:18:41.952: E/AndroidRuntime(6191): FATAL EXCEPTION: main
04-16 13:18:41.952: E/AndroidRuntime(6191): java.lang.RuntimeException: Unable to resume activity {spec.sartheris.myapp/spec.sartheris.myapp.MainActivity}: java.lang.NullPointerException
04-16 13:18:41.952: E/AndroidRuntime(6191):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2141)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2156)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at android.app.ActivityThread.access$1500(ActivityThread.java:121)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at android.os.Looper.loop(Looper.java:130)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at android.app.ActivityThread.main(ActivityThread.java:3701)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at java.lang.reflect.Method.invokeNative(Native Method)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at java.lang.reflect.Method.invoke(Method.java:507)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at dalvik.system.NativeStart.main(Native Method)
04-16 13:18:41.952: E/AndroidRuntime(6191): Caused by: java.lang.NullPointerException
04-16 13:18:41.952: E/AndroidRuntime(6191):     at spec.sartheris.myapp.MainActivity.onResume(MainActivity.java:47)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at android.app.Activity.performResume(Activity.java:3832)
04-16 13:18:41.952: E/AndroidRuntime(6191):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2131)
04-16 13:18:41.952: E/AndroidRuntime(6191):     ... 12 more
04-16 13:18:42.002: D/dalvikvm(6191): GC_CONCURRENT freed 54K, 49% free 2775K/5379K, external 2543K/5243K, paused 2ms+13ms

这是MainActivity中的代码

public class MainActivity extends Activity {
    MediaPlayer music;
    int length;
    ImageView img;
    AnimationDrawable animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        music = MediaPlayer.create(getApplicationContext(), R.raw.mymusic);
        music.setLooping(true);
        music.start();
        img = (ImageView) findViewById(R.id.imageView1);
        img.setBackgroundResource(R.drawable.myanim);
        animation = (AnimationDrawable) img.getDrawable();
        animation.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        music.pause();
        length = music.getCurrentPosition();
    }

    @Override
    protected void onResume() {
        super.onResume();
        music.seekTo(length);
        music.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.options, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.about:
            Intent ia = new Intent("spec.sartheris.myapp.ABOUTUS");
            startActivity(ia);
            break;
        case R.id.exit:
            finish();
            break;
        }
        return false;
    }
}

3 个答案:

答案 0 :(得分:0)

music未初始化。在onCreate()内初始化onResume()onCreate()之后运行{{1}}

答案 1 :(得分:0)

将此行移至onCreate()

music = MediaPlayer.create(getApplicationContext(), R.raw.mymusic);

或者您可以在onCreate()中添加此内容:

MediaPlayer music = new MediaPlayer();

然后在onWindowFocusChanged()中添加您的音乐文件:

music.setDataSource("PathToFile");

答案 2 :(得分:0)

出于某种原因,.aac文件没有在Gingerbread上播放,这就是错误发生的原因,用普通的.mp3文件替换它现在有效,很奇怪......