您好,我是 android 的新编码员。我有一个“播放”“暂停”和“停止”本地 .mp3 文件的程序。我在这里找到了这个网站的来源。
http://android-er.blogspot.com/2010/07/android-mediaplayer.html
所以我在代码中发布,一切似乎都很好,直到我真正点击播放并得到一个错误。
我试图阅读错误但无法弄清楚它到底在说什么?
我的错误是这个(logcat):
06-12 12:02:38.810: E/AndroidRuntime(4190): FATAL EXCEPTION: main
06-12 12:02:38.810: E/AndroidRuntime(4190): java.lang.NullPointerException
06-12 12:02:38.810: E/AndroidRuntime(4190):atcom.reg.ihigh.Cocaine$1.onClick(Cocaine.java:53)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.view.View.performClick(View.java:2485)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.view.View$PerformClick.run(View.java:9089)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.os.Handler.handleCallback(Handler.java:587)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.os.Handler.dispatchMessage(Handler.java:92)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.os.Looper.loop(Looper.java:123)
06-12 12:02:38.810: E/AndroidRuntime(4190): at android.app.ActivityThread.main(ActivityThread.java:3806)
06-12 12:02:38.810: E/AndroidRuntime(4190): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 12:02:38.810: E/AndroidRuntime(4190): at java.lang.reflect.Method.invoke(Method.java:507)
06-12 12:02:38.810: E/AndroidRuntime(4190): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-12 12:02:38.810: E/AndroidRuntime(4190): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-12 12:02:38.810: E/AndroidRuntime(4190): at dalvik.system.NativeStart.main(Native Method)
班级
package com.reg.ihigh;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Cocaine extends Activity {
MediaPlayer mediaPlayer;
Button buttonPlayPause, buttonQuit;
TextView textState;
private int stateMediaPlayer;
private final int stateMP_NotStarter = 0;
private final int stateMP_Playing = 1;
private final int stateMP_Pausing = 2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drugg);
buttonPlayPause = (Button)findViewById(R.id.playButton);
buttonQuit = (Button)findViewById(R.id.quitButton);
textState = (TextView)findViewById(R.id.state);
buttonPlayPause.setOnClickListener(buttonPlayPauseOnClickListener);
buttonQuit.setOnClickListener(buttonQuitOnClickListener);
initMediaPlayer();
}
private void initMediaPlayer()
{
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(Cocaine.this, R.raw.cocaine);
stateMediaPlayer = stateMP_NotStarter;
textState.setText("- IDLE -");
}
Button.OnClickListener buttonPlayPauseOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(stateMediaPlayer){
case stateMP_NotStarter:
mediaPlayer.start();
buttonPlayPause.setText("Pause");
textState.setText("- PLAYING -");
stateMediaPlayer = stateMP_Playing;
break;
case stateMP_Playing:
mediaPlayer.pause();
buttonPlayPause.setText("Play");
textState.setText("- PAUSING -");
stateMediaPlayer = stateMP_Pausing;
break;
case stateMP_Pausing:
mediaPlayer.start();
buttonPlayPause.setText("Pause");
textState.setText("- PLAYING -");
stateMediaPlayer = stateMP_Playing;
break;
}
}
};
Button.OnClickListener buttonQuitOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mediaPlayer.stop();
mediaPlayer.release();
finish();
}
};
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/playButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"/>
<Button
android:id="@+id/quitButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Quit"/>
<TextView
android:id="@+id/state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
编辑修复:
当我转换我的原始 mp3 以压缩它们时,它是如何在复制过程中损坏到原始文件夹的。所以我只是重新压缩复制过来并bam。一切正常!谢谢@MattWolfe