package com.RaffDev.RaffApp;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class RaffAppActivity extends Activity implements View.OnClickListener,``AdapterView.OnItemSelectedListener{
/*Regular Variables*/
String copyright = "Raff App \n\nVersion: Alpha 0.0.1\n\n(c) Copyright Raff App contributors and others 2011. All rights reserverd";
public int soundSelection;
MediaPlayer mp;
/*Called at the beginning of the application*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.soundEntries, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
/*Button Functions*/
public void onClick(View v){}
public void infoClick(View v){
Toast.makeText(this, copyright, Toast.LENGTH_LONG).show();
}
public void pressPlay(View v){
switch(soundSelection){
case 1:
if(mp.isPlaying())
mp.reset();
mp = MediaPlayer.create(getApplicationContext(), R.raw.pleaseprettyface);
mp.start();
break;
case 2:
if(mp.isPlaying())
mp.reset();
mp = MediaPlayer.create(getApplicationContext(),R.raw.sheeesh);
mp.start();
break;
case 3:
if(mp.isPlaying())
mp.reset();
mp = MediaPlayer.create(getApplicationContext(),R.raw.heaintdodat);
mp.start();
break;
case 4:
if(mp.isPlaying())
mp.reset();
mp = MediaPlayer.create(getApplicationContext(),R.raw.thedousky);
mp.start();
break;
case 5:
if(mp.isPlaying())
mp.reset();
mp = MediaPlayer.create(getApplicationContext(),R.raw.nmouth);
mp.start();
break;
}
}
/*Spinner Functions*/
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
soundSelection = (Integer) parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
答案 0 :(得分:1)
mp
为null。在pressPlay
中添加空检查,或在onCreate()
中创建一个mp实例。