gpt4 book ai didi

java - 单击图像时播放声音

转载 作者:行者123 更新时间:2023-12-01 14:24:28 25 4
gpt4 key购买 nike

我有一个图像,需要在单击它时播放 .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");

}

日志记录

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

最佳答案

在调用 findViewById() 之前,您需要在 onCreate() 中调用 setContentView(R.layout.your_layout)

否则,当您尝试膨胀 ImageView 时,它不知道要查找什么布局。

所以:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout); // <--- HERE, ADD THIS
ImageView view = (ImageView) findViewById(R.id.imageView1);

your_layout 替换为 .xml 布局文件的实际名称。

关于java - 单击图像时播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17277347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com