Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
59 views
in Technique[技术] by (71.8m points)

How to solve NullPointerException error in Android?

The below code runs smoothly in the emulator from Eclipse, but there are problems when I run it on my Android phone and tablet.

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);
}

I get a "NullPointException" in line "mp.start()". I have been working on this problem for a while, but I really do not know what is happening, I tried this on both my Android smartphone and tablet (Gingerbread and ICS), but the app force closes. I need help from you guys... thanks...

Logcat

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

Ringer Manifest

<?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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

NullPointerException means that mp is null. This means the MediaPlayer.create() failed. Does R.raw.alarm exist?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...