Ive的图像需要在单击时播放.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

最佳答案

您需要先在setContentView(R.layout.your_layout)中调用onCreate(),然后再调用findViewById()

否则,当您尝试为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布局文件的实际名称。

07-26 05:57