4

下面的代码在 Eclipse 的模拟器中运行顺畅,但在我的 Android 手机和平板电脑上运行时出现问题。

public class RingerActivity extends Activity{
/** Called when the activity is first created. */
Button press;
boolean tone = true;
MediaPlayer mp;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mp = new MediaPlayer();
    mp = MediaPlayer.create(RingerActivity.this, R.raw.alarm);
    //Error occurs at here
    mp.start();

    Handler h = new Handler();
    Runnable stopPlaybackRun = new Runnable() {
        public void run(){
            mp.stop();
            mp.release();
        }    
    };
    h.postDelayed(stopPlaybackRun, 20 * 1000);

    AudioManager manager = (AudioManager)getSystemService(AUDIO_SERVICE);
    if(manager!=null){
        manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        manager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){

    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
        Toast.makeText(this, "Volume Up", Toast.LENGTH_LONG).show();
        return true;
    }

    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
        Toast.makeText(this, "Volume Down", Toast.LENGTH_LONG).show();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

我在“mp.start()”行中得到一个“NullPointException”。我一直在研究这个问题一段时间,但我真的不知道发生了什么,我在我的 Android 智能手机和平板电脑(姜饼和 ICS)上都尝试了这个,但应用程序强制关闭。我需要你们的帮助...谢谢...

日志猫

11-09 15:41:09.909: E/AndroidRuntime(11975): FATAL EXCEPTION: main
11-09 15:41:09.909: E/AndroidRuntime(11975): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing.ringer/com.testing.ringer.RingerActivity}: java.lang.NullPointerException
11-09 15:41:09.909: E/AndroidRuntime(11975):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at android.os.Looper.loop(Looper.java:150)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at android.app.ActivityThread.main(ActivityThread.java:4385)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at java.lang.reflect.Method.invokeNative(Native Method)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at java.lang.reflect.Method.invoke(Method.java:507)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at dalvik.system.NativeStart.main(Native Method)
11-09 15:41:09.909: E/AndroidRuntime(11975): Caused by: java.lang.NullPointerException
11-09 15:41:09.909: E/AndroidRuntime(11975):    at com.testing.ringer.RingerActivity.onCreate(RingerActivity.java:23)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
11-09 15:41:09.909: E/AndroidRuntime(11975):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
11-09 15:41:09.909: E/AndroidRuntime(11975):    ... 11 more

林格清单

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

    <uses-sdk android:minSdkVersion="10" />

    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".RingerActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
4

3 回答 3

6

NullPointerException 表示 mp 为空。这意味着 MediaPlayer.create() 失败。R.raw.alarm 存在吗?

于 2012-11-09T07:01:25.877 回答
1

NullPointerException,导致当没有要使用的实例或数组时,最好跟踪到put breakpoint and debug your code

看起来你的代码很完美,只要确定它是否.mp3存在?你完美地保存在raw文件夹中。

最后尝试卸载您现有的,然后清理您的项目并运行。祝你好运。

于 2012-11-09T07:43:37.670 回答
1

您是否尝试清理项目?仅在清理项目后,新添加的资源才会显示在包资源管理器中。

于 2014-01-25T17:27:04.883 回答