R.id经常出错
经常出现这种情况,以前多运行几次就可以,现在不行
package bao.bao;
import java.security.PublicKey;
import android.R;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.*;
public class MainActivity extends Activity {
private Button bPlay;
private Button bPause;
private Button bStop;
private Button bAdd;
private Button bReduse;
private boolean pauseFlag=false;//暂停标志
MediaPlayer mp;
AudioManager am;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bPlay=(Button)findViewById(R.id.buttonpaly);
bPause=(Button)findViewById(R.id.buttonpause);
bStop=(Button)findViewById(R.id.buttonstop);
bAdd=(Button)findViewById(R.id.buttonvadd);
bReduse=(Button)findViewById(R.id.buttonvreduce);
mp=new MediaPlayer();
am=(AudioManager)this.getSystemService(this.AUDIO_SERVICE);
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mp.setDataSource("/sdcard/dl.mid");
} catch (Exception e) {
e.printStackTrace();
}
mp.start();
Toast.makeText(MainActivity.this,"播放音乐",Toast.LENGTH_LONG).show();
}
});
bPause.setOnClickListener(new View.OnClickListener() {//暂停
@Override
public void onClick(View v) {
if(mp.isPlaying()){
mp.pause();
pauseFlag=true;
}else if(pauseFlag){
mp.start();
pauseFlag=false;
Toast.makeText(MainActivity.this,"暂停播放",Toast.LENGTH_LONG).show();
}
}
});
bStop.setOnClickListener(new View.OnClickListener() {//停止
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.stop();
mp.reset();
try{
mp.setDataSource("/sdcard/dl.mid");
}catch (Exception e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "停止播放",Toast.LENGTH_LONG).show();
}
});
bAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
am.adjustVolume(AudioManager.ADJUST_RAISE,0);
System.out.println("faaa");
Toast.makeText(MainActivity.this,"增大音量",Toast.LENGTH_LONG).show();
}
});
bReduse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
am.adjustVolume(AudioManager.ADJUST_LOWER,0);
Toast.makeText(MainActivity.this,"减小音量",Toast.LENGTH_LONG).show();
}
});
}
}
<?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"
>
<Button
android:text="播放音乐"
android:id="@+id/buttonplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="暂停音乐"
android:id="@+id/buttonpause"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="停止音乐"
android:id="@+id/buttonstop"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="增大音乐"
android:id="@+id/buttonvadd"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="减小音乐"
android:id="@+id/buttonvreduce"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
R.layout.main R.id.buttonpaly R.id.buttonpause R.id.buttonstop R.id.buttonvadd R.id.buttonvreduce有红线
报错,怎样改??