2012-08-03 100 views
0

我正在嘗試編寫一個應用程序,單擊圖像時將播放聲音片段。剪輯大約1秒鐘。每當應用程序打開它強制關閉。這是我的編碼。它爲什麼迫近?提前Android - 按鈕單擊播放聲音 - 強制關閉

主要活動

package com.jeremy.vroom; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.media.MediaPlayer; 
import android.widget.Button; 
import android.view.View.OnClickListener; 
import android.view.View; 

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button zero = (Button)this.findViewById(R.id.btnZero); 
     zero.setOnClickListener(new OnClickListener() { 

      public void onClick(View view) {  
       new Thread(){ 
        public void run(){ 
         MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.rev); 
         mp.start(); 
        } 
       }.start(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

activitymain.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 



<ImageView 
    android:id="@+id/btnZero" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/uro5" 
    android:clickable="true" 
    android:contentDescription="@string/desc" ></ImageView> 

登錄

08-03 17:21:29.156: D/AndroidRuntime(272): Shutting down VM 
08-03 17:21:29.156: W/dalvikvm(272): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
08-03 17:21:29.298: E/AndroidRuntime(272): FATAL EXCEPTION: main 
08-03 17:21:29.298: E/AndroidRuntime(272): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jeremy.vroom/com.jeremy.vroom.MainActivity}: java.lang.ClassCastException: android.widget.ImageView 
08-03 17:21:29.298: E/AndroidRuntime(272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
08-03 17:21:29.298: E/AndroidRuntime(272): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
08-03 17:21:29.298: E/AndroidRuntime(272): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
08-03 17:21:29.298: E/AndroidRuntime(272): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
08-03 17:21:29.298: E/AndroidRuntime(272): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-03 17:21:29.298: E/AndroidRuntime(272): at android.os.Looper.loop(Looper.java:123) 
08-03 17:21:29.298: E/AndroidRuntime(272): at android.app.ActivityThread.main(ActivityThread.java:4627) 
08-03 17:21:29.298: E/AndroidRuntime(272): at java.lang.reflect.Method.invokeNative(Native Method) 
08-03 17:21:29.298: E/AndroidRuntime(272): at java.lang.reflect.Method.invoke(Method.java:521) 
08-03 17:21:29.298: E/AndroidRuntime(272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
08-03 17:21:29.298: E/AndroidRuntime(272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
08-03 17:21:29.298: E/AndroidRuntime(272): at dalvik.system.NativeStart.main(Native Method) 
08-03 17:21:29.298: E/AndroidRuntime(272): Caused by: java.lang.ClassCastException: android.widget.ImageView 
08-03 17:21:29.298: E/AndroidRuntime(272): at com.jeremy.vroom.MainActivity.onCreate(MainActivity.java:18) 
08-03 17:21:29.298: E/AndroidRuntime(272): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-03 17:21:29.298: E/AndroidRuntime(272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
08-03 17:21:29.298: E/AndroidRuntime(272): ... 11 more 
08-03 17:21:32.588: I/Process(272): Sending signal. PID: 272 SIG: 9 

感謝您的幫助

回答

1

使用

ImageView zero = (ImageView)findViewById(R.id.btnZero); 

代替,你聲明btnZero是一個ImageView的,而在你的代碼中引用它作爲一個按鈕。這會導致您的ClassCastException。嘗試使用:的

ImageView zero = (ImageView)findViewById(R.id.btnZero); 

代替

Button zero = (Button)this.findViewById(R.id.btnZero); 
+0

這工作。謝謝。無法弄清楚爲什麼常規按鈕不工作。 – rpcob 2012-08-03 18:06:29

1

嘗試改變:

Button zero = (Button)this.findViewById(R.id.btnZero); 

Button zero = (Button) findViewById(R.id.btnZero); 

看看是否有幫助。的

Button zero = (Button)this.findViewById(R.id.btnZero); 
1

在你的XML