我尝试创建使用声音的应用程序。 我遵循了本教程:https://www.youtube.com/watch?v=Fg7ib6S7ZkA。我创建了一个应用程序,它构建成功,没有错误,但是当我尝试在模拟器或设备上使用它时它崩溃了,只有消息“不幸'app_name'已停止”
我发现启动MediaPlayer变量 mpSplash.start();
时出现问题,因为当我评论此行时,一切正常(没有启动声音)
代码:
package com.example.nowa;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
MediaPlayer mpSplash = MediaPlayer.create(this,R.raw.soo);
mpSplash.start();
Thread logoTimer = new Thread(){
public void run() {
try{
int logoTimer = 0;
while (logoTimer <5000){
sleep(100);
logoTimer = logoTimer + 100;
}
startActivity(new Intent("com.example.nowa.CLEARSCREEN"));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
}
第2次活动
package com.example.nowa;
import android.app.Activity; import android.os.Bundle;
公共类myMenu扩展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nowa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.nowa.MainActivity"
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="com.example.nowa.myMenu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.nowa.CLEARSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat信息:
05-05 17:25:57.626: E/MediaPlayer(638): error (1, -2147483648)
05-05 17:25:57.676: E/AndroidRuntime(638): FATAL EXCEPTION: main
05-05 17:25:57.676: E/AndroidRuntime(638): Process: com.example.nowa, PID: 638
05-05 17:25:57.676: E/AndroidRuntime(638): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nowa/com.example.nowa.MainActivity}: java.lang.NullPointerException
05-05 17:25:57.676: E/AndroidRuntime(638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
05-05 17:25:57.676: E/AndroidRuntime(638): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
05-05 17:25:57.676: E/AndroidRuntime(638): at android.app.ActivityThread.access$800(ActivityThread.java:138)
05-05 17:25:57.676: E/AndroidRuntime(638): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
05-05 17:25:57.676: E/AndroidRuntime(638): at android.os.Handler.dispatchMessage(Handler.java:102)
05-05 17:25:57.676: E/AndroidRuntime(638): at android.os.Looper.loop(Looper.java:136)
05-05 17:25:57.676: E/AndroidRuntime(638): at android.app.ActivityThread.main(ActivityThread.java:5042)
05-05 17:25:57.676: E/AndroidRuntime(638): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 17:25:57.676: E/AndroidRuntime(638): at java.lang.reflect.Method.invoke(Method.java:515)
05-05 17:25:57.676: E/AndroidRuntime(638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
05-05 17:25:57.676: E/AndroidRuntime(638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-05 17:25:57.676: E/AndroidRuntime(638): at dalvik.system.NativeStart.main(Native Method)
05-05 17:25:57.676: E/AndroidRuntime(638): Caused by: java.lang.NullPointerException
05-05 17:25:57.676: E/AndroidRuntime(638): at com.example.nowa.MainActivity.onCreate(MainActivity.java:28)
05-05 17:25:57.676: E/AndroidRuntime(638): at android.app.Activity.performCreate(Activity.java:5231)
05-05 17:25:57.676: E/AndroidRuntime(638): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-05 17:25:57.676: E/AndroidRuntime(638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
05-05 17:25:57.676: E/AndroidRuntime(638): ... 11 more