0

我已经编写了一个特定时间报警电话的代码,并且警报将在该时间段后停止。我试过在模拟器中运行这段代码,一切运行顺利。但是当我在我的 HTC Desire HD (Android 2.3.5) 上尝试这个时,应用程序被强制关闭。谁能告诉我发生了什么???

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);
    try {
        mp.prepare();

    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (Exception e) {
        Toast.makeText(this, e.getMessage() , Toast.LENGTH_SHORT).show();         }
    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);
    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);
}

日志猫

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

4 回答 4

1

该问题是由于 onCreate 处的空指针异常引起的。有时音频服务不会返回 AudioManager。

更改代码:

AudioManager manager = (AudioManager)getSystemService(AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
manager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);

如下 onCreate :

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);
}
于 2012-11-09T06:09:37.673 回答
0

尝试使用此代码,

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

    mp = MediaPlayer.create(RingerActivity.this, R.raw.alarm);
    // are you sure that res/raw/alarm is a valid media file?
    mp.start();
于 2012-11-09T06:01:23.437 回答
0

您的活动中有一条NullPointerException在线 35。修理它。

于 2012-11-09T06:03:36.660 回答
0
MediaPlayer mp = MediaPlayer.create(context, R.raw.alarm);
mp.prepare(); <- Error, delete this line in your code

您无需调用方法“prepare();”、方法“create(,);” 为你做。:)

于 2016-12-21T08:23:01.253 回答