Android,threadid = 1:线程退出,未捕获异常(group = 0xa4db4b20)

时间:2014-08-31 07:25:39

标签: android nullpointerexception fatal-error

我正在编写使用android.media.MediaPlayer在Android上播放mp3的简单代码,但我收到此错误

  

线程以未捕获的异常(group = 0xa4db4b20)和NPE

退出

这是我的MainActivity.java: -

public class MainActivity extends ActionBarActivity {

private TextView textView;
private ImageButton button;
private MediaPlayer player;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    onGo();
}

private void onGo() {

    button=(ImageButton) findViewById(R.id.imageButton1);
    textView = (TextView) findViewById(R.id.textView1);
    player.create(MainActivity.this, R.raw.kalimba);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            player.start();     
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

和我的Logcat: -

08-31 06:57:27.647: E/OpenGLRenderer(1237): Getting MAX_TEXTURE_SIZE from GradienCache
08-31 06:57:27.651: E/OpenGLRenderer(1237): MAX_TEXTURE_SIZE: 8192
08-31 06:57:27.655: E/OpenGLRenderer(1237): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
08-31 06:57:27.655: E/OpenGLRenderer(1237): MAX_TEXTURE_SIZE: 8192
08-31 06:57:27.655: D/OpenGLRenderer(1237): Enabling debug mode 0
08-31 06:57:29.039: D/AndroidRuntime(1237): Shutting down VM
08-31 06:57:29.039: W/dalvikvm(1237): threadid=1: thread exiting with uncaught exception (group=0xa4db4b20)
08-31 06:57:29.051: E/AndroidRuntime(1237): FATAL EXCEPTION: main
08-31 06:57:29.051: E/AndroidRuntime(1237): Process: com.example.player, PID: 1237
08-31 06:57:29.051: E/AndroidRuntime(1237): java.lang.NullPointerException
08-31 06:57:29.051: E/AndroidRuntime(1237): at com.example.player.MainActivity$1.onClick(MainActivity.java:44)
08-31 06:57:29.051: E/AndroidRuntime(1237):     at android.view.View.performClick(View.java:4438)

1 个答案:

答案 0 :(得分:1)

您获得NullPointerException,因为playeronGo()为空。此外,create()MediaPlayer的静态方法,您应该像这样使用它:

 player = MediaPlayer.create(MainActivity.this, R.raw.kalimba);