我試圖製作一個非常簡單的音頻播放器,但後來當應用程序在模擬器上運行時,單擊播放按鈕時彈出錯誤「停止工作「,並墜毀。不幸的是,當應用程序在genymotion模擬器上運行時,android已停止工作
這是我的代碼。
MainActivity.java
package toast.akappstudio.com.audiodemo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
MediaPlayer player;
public void playAudio(View view){
player.start();
}
public void pauseAudio(View view){
player.pause();
}
public void stopAudio(View view){
player.stop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaPlayer player = MediaPlayer.create(this, R.raw.audio);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} }
的content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="toast.akappstudio.com.audiodemo.MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="@+id/playbutton"
android:layout_marginLeft="67dp"
android:layout_marginStart="67dp"
android:layout_marginTop="176dp"
android:onClick="playAudio"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:id="@+id/pausebutton"
android:layout_marginLeft="55dp"
android:layout_marginStart="55dp"
android:onClick="pauseAudio"
android:layout_alignTop="@+id/playbutton"
android:layout_toRightOf="@+id/playbutton"
android:layout_toEndOf="@+id/playbutton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STOP"
android:id="@+id/stopbutton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="stopAudio" />
</RelativeLayout>
的logcat:
java.lang.IllegalStateException:無法 Android的執行方法:的onClick
在 android.support.v7.app .AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275) at android.view.View.performClick(View.java:4438) at android.view.View $ PerformClick.run(View.java:18422) at android .os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app .ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart。 main(Native Method) 由java.lang.reflect.Method.invoke(Method.java:515) (java.lang.reflect.Method.invokeNative)(Native Method) 引發:java.lang.reflect.InvocationTargetException 在 android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) at android.view.View.performClick(View.java:4438) at android.view.View $ PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java。 lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main (本地方法) 由於: 在java.lang.reflect.Method。invokeNative(本地方法) at java.lang.reflect.Method.invoke(Method.java:515) at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) at android。 view.View.performClick(View.java:4438) at android.view.View $ PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android。 os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run( ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) 06-28 13:40:39.326 2477- 2477 /?我/過程:發送信號。 PID:2477 SIG:9
解釋爲什麼它會變得幾乎完美。 –
@EugenPechanec我已經更新了原因:) – SripadRaj
@Adi_Kat我已經更新了原因的答案 – SripadRaj