我是编程新手,在过去的几个小时里一直试图解决这个问题。我知道这是愚蠢的事。我试图让每个按钮发出声音。任何帮助是极大的赞赏。
(增加"import android.view.View.OnClickListener;"
,把我的问题变成了绿色,直到我用#34;"然后#34;然后将进口变为灰色。)
public class PocketParent extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer stopMP = MediaPlayer.create(this, R.raw.btmg);
final MediaPlayer dropMP = MediaPlayer.create(this, R.raw.knock);
final MediaPlayer popMP = MediaPlayer.create(this, R.raw.oldsoulwr);
final MediaPlayer noMP = MediaPlayer.create(this, R.raw.smpr);
final MediaPlayer yeahMP = MediaPlayer.create(this, R.raw.upbeaty);
ImageButton playstop = (ImageButton) this.findViewById(R.id.imageButton);
ImageButton playdrop = (ImageButton) this.findViewById(R.id.imageButton2);
ImageButton playpop = (ImageButton) this.findViewById(R.id.imageButton3);
ImageButton playno = (ImageButton) this.findViewById(R.id.imageButton4);
ImageButton playyeah = (ImageButton) this.findViewById(R.id.imageButton5);
// (HERE IS WHERE MY PROBLEM LIES)
// *OnClickListener is abstract, cannot be instantiated.*
// (IT ALSO SAYS IT'S EXPECTING A "," OR " ' " RIGHT BEFORE "new",
// but that doesn't work either.)
playstop.setOnClickListener(new View.OnClickListener()
playdrop.setOnClickListener(new View.OnClickListener()
playpop.setOnClickListener(new View.OnClickListener()
playno.setOnClickListener(new View.OnClickListener()
playyeah.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopMP.start();
dropMP.start();
popMP.start();
noMP.start();
yeahMP.start();
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
}
....
}
}
答案 0 :(得分:2)
你不能这样做:
playstop.setOnClickListener(new View.OnClickListener()
playdrop.setOnClickListener(new View.OnClickListener()
playpop.setOnClickListener(new View.OnClickListener()
....
因为这意味着在java中,你传递给playstop方法很多参数没有逗号分割,
执行
语法更有意义playstop.setOnClickListener(new View.OnClickListener());
playdrop.setOnClickListener(new View.OnClickListener());
playpop.setOnClickListener(new View.OnClickListener());
....
但您无法做到这一点,因为View.OnClickListener
是界面
创建View.OnClickListener
的对象只是没有意义......
你需要匿名实现所有按钮上的界面,就像你使用playYeah按钮一样
playyeah.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
....
}
});
playstop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
....
}
});
playdrop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
....
}
});
Etc etc
答案 1 :(得分:-2)
尝试更改
ImageButton playstop = (ImageButton) this.findViewById(R.id.imageButton);
ImageButton playdrop = (ImageButton) this.findViewById(R.id.imageButton2);
ImageButton playpop = (ImageButton) this.findViewById(R.id.imageButton3);
ImageButton playno = (ImageButton) this.findViewById(R.id.imageButton4);
ImageButton playyeah = (ImageButton) this.findViewById(R.id.imageButton5);
到
ImageButton playstop = (ImageButton) findViewById(R.id.imageButton);
ImageButton playdrop = (ImageButton) findViewById(R.id.imageButton2);
ImageButton playpop = (ImageButton) findViewById(R.id.imageButton3);
ImageButton playno = (ImageButton) findViewById(R.id.imageButton4);
ImageButton playyeah = (ImageButton) findViewById(R.id.imageButton5);
希望它能帮到你!!