经常出现这种情况,以前多运行几次就可以,现在不行
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有红线报错,怎样改??

解决方案 »

  1.   

    1、检查drawable目录中的文件是否有重名,是否有大写;2、检查Layout目录中是否有文件出错3、菜单Project -> Clean
      

  2.   

    android:text试下引用string.xml里的值
      

  3.   

    import android.R;
    看到了这一行,大概知道为什么出错了,
    你的R.java是不是不在bao.bao这个包下?
    以为不是在同一个包下,所以你在使用R.id.xxx时eclipse会自动帮你导入系统自带的android.R包,导致你的res里的东西都无法读取;
    只要把import android.R;
    改成import [R.java文件所在的包].R;
    就行了
      

  4.   

    直接把代码中的import android.R;删掉应该就没问题了,这个不用显式应用的