在mediaPlayer类上调用函数start()的异常

时间:2013-04-25 21:11:59

标签: android

我正在使用Android的MediaPlayer类,我在activity_main.xml上进行布局,如下所示: -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/screen" android:layout_width="fill_parent" android:layout_height="fill_parent" >
    <LinearLayout android:orientation="vertical" android:layout_alignParentTop="true" 
        android:layout_alignParentLeft="true" android:layout_width="fill_parent" android:layout_height="fill_parent" 
        android:weightSum="7" >
        <LinearLayout android:layout_weight="6" android:orientation="vertical" android:layout_alignParentTop="true" 
            android:layout_alignParentLeft="true" android:layout_width="fill_parent" android:layout_height="0dip" >
            <ImageView android:id="@+id/bg" android:layout_width="fill_parent" android:layout_height="fill_parent" >
            </ImageView>
        </LinearLayout>
        <LinearLayout android:layout_weight="1" android:id="@+id/buttons" android:orientation="horizontal" android:weightSum="5" 
            android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:gravity="center" 
            android:layout_width="fill_parent" android:layout_height="0dip" android:background="#FFFFFF" >
            <Button android:id="@+id/btnPrevious" android:layout_width="0dip" android:layout_height="fill_parent" 
                android:layout_weight="1" android:background="@drawable/previous" android:layout_margin="5dp" 
                android:onClick="pauseradio" />
            <View android:layout_weight="1" android:layout_width="0dip" android:layout_height="0dip" />
            <Button android:id="@+id/btnPlay" android:layout_width="0dip" android:layout_height="fill_parent" 
                android:layout_weight="1" android:background="@drawable/play" android:layout_margin="5dp" 
                android:onClick="startradio" />
            <View android:layout_weight="1" android:layout_width="0dip" android:layout_height="0dip" />
            <Button android:id="@+id/btnNext" android:layout_width="0dip" android:layout_height="fill_parent" 
                android:layout_weight="1" android:background="@drawable/next" android:layout_margin="5dp" 
                android:onClick="stopradio" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

我为我的应用程序设置了bg但没有出现在它上面,然后我按下按钮然后停止和上一次用于我的媒体但是没有工作。

我在按钮上调用它的功能如下: -

public void startradio() {
    mp.start();
}

public void pauseradio() {
    mp.pause();
}

public void stopradio() {
    mp.stop();
}

修改: -

iam使用MediaPlayer类但是当我的应用程序启动时出现此错误

04-25 19:06:23.323: I/dalvikvm(652): threadid=3: reacting to signal 3
04-25 19:06:23.352: I/dalvikvm(652): Wrote stack traces to '/data/anr/traces.txt'
04-25 19:07:52.862: D/gralloc_goldfish(702): Emulator without GPU emulation detected.
04-25 19:07:52.892: W/TextLayoutCache(702): computeValuesWithHarfbuzz -- need to force to single run
04-25 19:08:32.603: D/AndroidRuntime(702): Shutting down VM
04-25 19:08:32.603: W/dalvikvm(702): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-25 19:08:32.633: E/AndroidRuntime(702): FATAL EXCEPTION: main
04-25 19:08:32.633: E/AndroidRuntime(702): java.lang.IllegalStateException: Could not find a method pauseradio(View) in the activity class com.example.kam.MainActivity for onClick handler on view class android.widget.Button with id 'btnPrevious'
04-25 19:08:32.633: E/AndroidRuntime(702):  at android.view.View$1.onClick(View.java:3026)
04-25 19:08:32.633: E/AndroidRuntime(702):  at android.view.View.performClick(View.java:3480)
04-25 19:08:32.633: E/AndroidRuntime(702):  at android.view.View$PerformClick.run(View.java:13983)
04-25 19:08:32.633: E/AndroidRuntime(702):  at android.os.Handler.handleCallback(Handler.java:605)
04-25 19:08:32.633: E/AndroidRuntime(702):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 19:08:32.633: E/AndroidRuntime(702):  at android.os.Looper.loop(Looper.java:137)
04-25 19:08:32.633: E/AndroidRuntime(702):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-25 19:08:32.633: E/AndroidRuntime(702):  at java.lang.reflect.Method.invokeNative(Native Method)
04-25 19:08:32.633: E/AndroidRuntime(702):  at java.lang.reflect.Method.invoke(Method.java:511)
04-25 19:08:32.633: E/AndroidRuntime(702):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-25 19:08:32.633: E/AndroidRuntime(702):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-25 19:08:32.633: E/AndroidRuntime(702):  at dalvik.system.NativeStart.main(Native Method)
04-25 19:08:32.633: E/AndroidRuntime(702): Caused by: java.lang.NoSuchMethodException: pauseradio [class android.view.View]
04-25 19:08:32.633: E/AndroidRuntime(702):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-25 19:08:32.633: E/AndroidRuntime(702):  at java.lang.Class.getMethod(Class.java:915)
04-25 19:08:32.633: E/AndroidRuntime(702):  at android.view.View$1.onClick(View.java:3019)
04-25 19:08:32.633: E/AndroidRuntime(702):  ... 11 more

我如何在Android上的应用程序上调用此函数。

2 个答案:

答案 0 :(得分:1)

您需要更改方法的签名

public void startradio(View v) {
    mp.start();
}

public void pauseradio(View v) {
    mp.pause();
}

public void stopradio(View v) {
    mp.stop();
}

答案 1 :(得分:0)

您是否在媒体播放器对象上调用了Prepare?

http://developer.android.com/reference/android/media/MediaPlayer.html

查看生命周期信息,如果从异常中发布堆栈跟踪,它也会有所帮助。