0

我的应用程序不想在 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;
    }
}
4

3 回答 3

0

music未初始化。在onCreate()运行onResume()后初始化它onCreate()

于 2013-04-16T10:31:10.157 回答
0

将此行移到onCreate()

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

或者您可以添加它onCreate()

MediaPlayer music = new MediaPlayer();

然后以onWindowFocusChanged()这种方式添加您的音乐文件:

music.setDataSource("PathToFile");
于 2013-04-16T12:30:23.037 回答
0

由于某种原因,.aac 文件没有在 Gingerbread 上播放,这就是错误发生的原因,将其替换为普通的 .mp3 文件,现在可以正常工作了,很奇怪......

于 2013-04-16T13:50:00.270 回答