Android release()方法导致程序意外停止

时间:2014-07-22 00:43:33

标签: java android

我是Android的新手我正在构建一个应用程序,我有2个布局,其中一个可见5秒钟然后其他布局显示它工作正常,直到我在代码中添加了一个音轨,它现在关闭了程序一旦5秒结束,我删除了release();方法,程序运行良好,但没有它,但当然歌曲将继续播放!!

所以这里是代码和日志,并提前感谢。

首先清单: -

 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.badr.backgroundandsound"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".LayoutClass"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.badr.backgroundandsound.MAINACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

现在使用java类:

package com.badr.backgroundandsound;

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

public class LayoutClass extends Activity {
    MediaPlayer nasheed;
    @Override
    protected void onCreate(Bundle x) {
        // TODO Auto-generated method stub
        super.onCreate(x);
        setContentView(R.layout.intro);

        MediaPlayer nasheed = MediaPlayer.create(LayoutClass.this, R.raw.yamanyara);
        nasheed.start();

        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                }catch(InterruptedException e ){
                    e.printStackTrace();
                }finally{
                    Intent nextActivity = new Intent("com.badr.backgroundandsound.MAINACTIVITY");
                    startActivity(nextActivity);
                }
            }
        };
        timer.start();
    }

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


}

现在是日志:

07-22 03:29:46.621: E/AndroidRuntime(824): FATAL EXCEPTION: main
07-22 03:29:46.621: E/AndroidRuntime(824): java.lang.RuntimeException: Unable to pause activity {com.badr.backgroundandsound/com.badr.backgroundandsound.LayoutClass}: java.lang.NullPointerException
07-22 03:29:46.621: E/AndroidRuntime(824):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3348)
07-22 03:29:46.621: E/AndroidRuntime(824):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3305)
07-22 03:29:46.621: E/AndroidRuntime(824):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3288)
07-22 03:29:46.621: E/AndroidRuntime(824):  at android.app.ActivityThread.access$2500(ActivityThread.java:125)
07-22 03:29:46.621: E/AndroidRuntime(824):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
07-22 03:29:46.621: E/AndroidRuntime(824):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 03:29:46.621: E/AndroidRuntime(824):  at android.os.Looper.loop(Looper.java:123)
07-22 03:29:46.621: E/AndroidRuntime(824):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-22 03:29:46.621: E/AndroidRuntime(824):  at java.lang.reflect.Method.invokeNative(Native Method)
07-22 03:29:46.621: E/AndroidRuntime(824):  at java.lang.reflect.Method.invoke(Method.java:521)
07-22 03:29:46.621: E/AndroidRuntime(824):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-22 03:29:46.621: E/AndroidRuntime(824):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-22 03:29:46.621: E/AndroidRuntime(824):  at dalvik.system.NativeStart.main(Native Method)
07-22 03:29:46.621: E/AndroidRuntime(824): Caused by: java.lang.NullPointerException
07-22 03:29:46.621: E/AndroidRuntime(824):  at com.badr.backgroundandsound.LayoutClass.onPause(LayoutClass.java:38)
07-22 03:29:46.621: E/AndroidRuntime(824):  at android.app.Activity.performPause(Activity.java:3842)
07-22 03:29:46.621: E/AndroidRuntime(824):  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1190)
07-22 03:29:46.621: E/AndroidRuntime(824):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3335)

0 个答案:

没有答案