我有一个图像,当您单击它时需要播放 .wav 文件。但是该应用程序根本没有启动。我不知道为什么,因为它看起来我声明了它需要的所有东西。但是当我启动应用程序时它立即崩溃
public class MainActivity extends Activity {
private SoundPool soundPool;
private int soundID;
boolean loaded = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView view = (ImageView) findViewById(R.id.imageView1);
view.setOnTouchListener((OnTouchListener) this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.koe, 1);
}
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
return false;
}
编辑
我认为你必须这样做
// Declare as global variable
WebView mWebview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayoutfile);
Toast.makeText(this, "Laden van roosterwijzigingen.",
Toast.LENGTH_SHORT).show();
mWebview =(WebView) findviewById(R.id.webview_id);
// Enable JavaScript
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
}
LOGCAT
06-24 19:46:07.979: E/AndroidRuntime(10368): FATAL EXCEPTION: main
06-24 19:46:07.979: E/AndroidRuntime(10368): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.flikkema.robin.vleesapp/org.flikkema.robin.vleesapp.MainActivity}: java.lang.ClassCastException: org.flikkema.robin.vleesapp.MainActivity cannot be cast to android.view.View$OnTouchListener
06-24 19:46:07.979: E/AndroidRuntime(10368): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-24 19:46:07.979: E/AndroidRuntime(10368): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-24 19:46:07.979: E/AndroidRuntime(10368): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-24 19:46:07.979: E/AndroidRuntime(10368): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-24 19:46:07.979: E/AndroidRuntime(10368): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 19:46:07.979: E/AndroidRuntime(10368): at android.os.Looper.loop(Looper.java:137)
06-24 19:46:07.979: E/AndroidRuntime(10368): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-24 19:46:07.979: E/AndroidRuntime(10368): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 19:46:07.979: E/AndroidRuntime(10368): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 19:46:07.979: E/AndroidRuntime(10368): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-24 19:46:07.979: E/AndroidRuntime(10368): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-24 19:46:07.979: E/AndroidRuntime(10368): at dalvik.system.NativeStart.main(Native Method)
06-24 19:46:07.979: E/AndroidRuntime(10368): Caused by: java.lang.ClassCastException: org.flikkema.robin.vleesapp.MainActivity cannot be cast to android.view.View$OnTouchListener
06-24 19:46:07.979: E/AndroidRuntime(10368): at org.flikkema.robin.vleesapp.MainActivity.onCreate(MainActivity.java:24)
06-24 19:46:07.979: E/AndroidRuntime(10368): at android.app.Activity.performCreate(Activity.java:5008)
06-24 19:46:07.979: E/AndroidRuntime(10368): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-24 19:46:07.979: E/AndroidRuntime(10368): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-24 19:46:07.979: E/AndroidRuntime(10368): ... 11 more