#2
icanbestrong2016-01-25 20:43
|
package com.example.ilovedyou_;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
public Button[][] Btn;
private SoundPool sp;
private int music1,music2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
for(int i=0;i<6;i++)
for(int j=0;j<9;j++)
{
Btn[i][j]=(Button)findViewById(R.id.Btn_00+9*i+j);
}
sp=new SoundPool(5,AudioManager.STREAM_SYSTEM,5);
music1=sp.load(this,R.raw.cat,1);
music2=sp.load(this,R.raw.dog,2);
for(int i=0;i<6;i++)
for(int j=0;j<9;j++)
{
Btn[i][j].setOnClickListener(listener);
}
}
private OnClickListener listener=new OnClickListener(){
public void onClick(View v){
if(v==Btn[0][1]||v==Btn[0][2]||v==Btn[0][5]||v==Btn[0][8]
||v==Btn[1][0]||v==Btn[1][4]||v==Btn[1][6]||v==Btn[1][8]
||v==Btn[2][0]||v==Btn[2][4]||v==Btn[2][6]||v==Btn[2][8]
||v==Btn[3][0]||v==Btn[3][4]||v==Btn[3][5]||v==Btn[3][6]||v==Btn[3][8]
||v==Btn[4][0]||v==Btn[4][4]||v==Btn[4][6]||v==Btn[4][8]
||v==Btn[5][1]||v==Btn[5][2]||v==Btn[5][4]||v==Btn[5][6]||v==Btn[5][8])
{
sp.play(music1,1,1,0,0,1);
}
else
{
sp.play(music2,1,1,1,1,1);
}
}};
@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;
}
}
logcat显示是01-25 07:25:33.490: E/AndroidRuntime(1384): Caused by: java.lang.NullPointerException
再次求大牛指点