从R中的教程源结果导入项目无法解析为变量

时间:2013-12-17 18:38:12

标签: java android eclipse r.java-file

我不确定为什么但是当我尝试导入这个项目时,我最终还是在编辑项目之前无法解决变量错误。我发现很难相信作者会发布一个不能编译的源代码示例 - 这可能是我最终可能使用eclipse吗?

http://alucard1990.hubpages.com/hub/How-to-Make-a-Simple-Media-Player-for-Android

R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 298    Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 302    Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 287    Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 294    Java Problem
error: No resource identifier found for attribute 'onClick' in package 'android'    main.xml    /MyMediaPlayer/res/layout   line 15 Android AAPT Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 307    Java Problem
error: No resource identifier found for attribute 'onClick' in package 'android'    main.xml    /MyMediaPlayer/res/layout   line 19 Android AAPT Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 312    Java Problem
error: No resource identifier found for attribute 'onClick' in package 'android'    main.xml    /MyMediaPlayer/res/layout   line 23 Android AAPT Problem
DIRECTORY_PICTURES cannot be resolved or is not a field MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 118    Java Problem
DIRECTORY_MUSIC cannot be resolved or is not a field    MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 117    Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 90 Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 284    Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 281    Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 278    Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 253    Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 89 Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 88 Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 74 Java Problem
R cannot be resolved to a variable  MyMediaPlayerActivity.java  /MyMediaPlayer/src/com/technegames/mymediaplayer    line 55 Java Problem

来源:

public class MyMediaPlayerActivity extends Activity {
    WakeLock wakeLock;
    private static final String[] EXTENSIONS = { ".mp3", ".mid", ".wav", ".ogg", ".mp4" }; //Playable Extensions
    List<String> trackNames; //Playable Track Titles
    List<String> trackArtworks; //Track artwork names
    AssetManager assets; //Assets (Compiled with APK)
    File path; //directory where music is loaded from on SD Card
    File path2; //directory where album artwork is loaded from on SD Card
    Music track; //currently loaded track
    ImageView bg; //Track artwork
    Button btnPlay; //The play button will need to change from 'play' to 'pause', so we need an instance of it
    Random random; //used for shuffle
    boolean shuffle; //is shuffle mode on?
    boolean isTuning; //is user currently jammin out, if so automatically start playing the next track
    int currentTrack; //index of current track selected
    int type; //0 for loading from assets, 1 for loading from SD card

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "Lexiconda");
        setContentView(R.layout.main);

        initialize(0);
    }

    @Override
    public void onResume(){
        super.onResume();
        wakeLock.acquire();
    }

    @Override
    public void onPause(){
        super.onPause();
        wakeLock.release();
        if(track != null){
            if(track.isPlaying()){
                track.pause();
                isTuning = false;
                btnPlay.setBackgroundResource(R.drawable.play);
            }
            if(isFinishing()){
                track.dispose();
                finish();
            }
        } else{
            if(isFinishing()){
                finish();
            }
        }
    }

    private void initialize(int type){
        bg = (ImageView) findViewById(R.id.bg);
        btnPlay = (Button) findViewById(R.id.btnPlay);
        btnPlay.setBackgroundResource(R.drawable.play);
        trackNames = new ArrayList<String>();
        trackArtworks = new ArrayList<String>();
        assets = getAssets();
        currentTrack = 0;
        shuffle = false;
        isTuning = false;
        random = new Random();
        this.type = type;

        addTracks(getTracks());
        loadTrack();
    }

    //Generate a String Array that represents all of the files found
    private String[] getTracks(){
        if(type == 0){
            try {
                String[] temp = getAssets().list("");
                return temp;
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        } else if(type == 1){
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) 
                    || Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)){
                path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
                path2 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                String[] temp = path.list();
                return temp;
            } else{
                Toast.makeText(getBaseContext(), "SD Card is either mounted elsewhere or is unusable", Toast.LENGTH_LONG).show();
            }
        }
        return null;
    }

    //Adds the playable files to the trackNames List
    private void addTracks(String[] temp){
        if(temp != null){
            for(int i = 0; i < temp.length; i++){
                //Only accept files that have one of the extensions in the EXTENSIONS array
                if(trackChecker(temp[i])){
                    trackNames.add(temp[i]);
                    trackArtworks.add(temp[i].substring(0, temp[i].length()-4));
                }
            }
            Toast.makeText(getBaseContext(), "Loaded " + Integer.toString(trackNames.size()) + " Tracks", Toast.LENGTH_SHORT).show();
        }
    }

    //Checks to make sure that the track to be loaded has a correct extenson
    private boolean trackChecker(String trackToTest){
        for(int j = 0; j < EXTENSIONS.length; j++){
            if(trackToTest.contains(EXTENSIONS[j])){
                return true;
            }
        }
        return false;
    }

    //Loads the track by calling loadMusic
    private void loadTrack(){
        if(track != null){
            track.dispose();
        }
        if(trackNames.size() > 0){
            track = loadMusic(type);
            setImage("drawable/" + trackArtworks.get(currentTrack));
        }
    }

    //loads a Music instance using either a built in asset or an external resource
    private Music loadMusic(int type){
        switch(type){
        case 0:
            try{
                AssetFileDescriptor assetDescriptor = assets.openFd(trackNames.get(currentTrack));
                return new Music(assetDescriptor);
            } catch(IOException e){
                e.printStackTrace();
                Toast.makeText(getBaseContext(), "Error Loading " + trackNames.get(currentTrack), Toast.LENGTH_LONG).show();
            }
            return null;
        case 1:
            try{
                FileInputStream fis = new FileInputStream(new File(path, trackNames.get(currentTrack)));
                FileDescriptor fileDescriptor = fis.getFD();
                return new Music(fileDescriptor);
            } catch(IOException e){
                e.printStackTrace();
                Toast.makeText(getBaseContext(), "Error Loading " + trackNames.get(currentTrack), Toast.LENGTH_LONG).show();
            }
            return null;
        default:
            return null;
        }
    }

    //Sets the background image to match the track currently playing or a default image
    private void setImage(String name) {
        if(type == 0){
            int imageResource = getResources().getIdentifier(name, null, getPackageName());
            if(imageResource != 0){
                Drawable image = getResources().getDrawable(imageResource);
                bg.setImageDrawable(image);
            } else{
                int defaultImageResource = getResources().getIdentifier("drawable/defaultbg", null, getPackageName());
                if(defaultImageResource != 0){
                    Drawable image = getResources().getDrawable(defaultImageResource);
                    bg.setImageDrawable(image);
                }
            }
        } else if(type == 1){
            if(new File(path2.getAbsolutePath(), trackArtworks.get(currentTrack) + ".jpg").exists()){
                bg.setImageDrawable(Drawable.createFromPath(path2.getAbsolutePath() + "/" + trackArtworks.get(currentTrack) + ".jpg"));
            } else{
                int defaultImageResource = getResources().getIdentifier("drawable/defaultbg", null, getPackageName());
                if(defaultImageResource != 0){
                    Drawable image = getResources().getDrawable(defaultImageResource);
                    bg.setImageDrawable(image);
                }
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
        createMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case 0:
            //Set Looping
            synchronized(this){
                if(track.isLooping()){
                    track.setLooping(false);
                    Toast.makeText(getBaseContext(), "Playing Tracks Sequentially", Toast.LENGTH_SHORT).show();
                } else{
                    track.setLooping(true);
                    Toast.makeText(getBaseContext(), "Looping " + trackNames.get(currentTrack), Toast.LENGTH_SHORT).show();
                }
            }
            return true;
        case 1:
            //Set Shuffle
            synchronized(this){
                if(shuffle){
                    setShuffle(false);
                } else{
                    setShuffle(true);
                }
            }
            return true;
        case 2:
            //Stop Music
            synchronized(this){
                track.switchTracks();
                btnPlay.setBackgroundResource(R.drawable.play);
            }
            return true;
        case 3:
            //Change Source from Assets to SD Card and vice versa
            synchronized(this){
                type++;
                if(type > 1){
                    type = 0;
                }
            }
            if(type == 0){
                Toast.makeText(getBaseContext(), "Loading Tracks from Assets ", Toast.LENGTH_SHORT).show();
            } else if(type == 1){
                Toast.makeText(getBaseContext(), "Loading Tracks from SD Card", Toast.LENGTH_SHORT).show();
            }
            initialize(type);
            return true;
        default:
            return false;
        }
    }

    private void createMenu(Menu menu){
        MenuItem miLooping = menu.add(0, 0, 0, "Looping");{
            miLooping.setIcon(R.drawable.looping);
        }
        MenuItem miShuffle = menu.add(0, 1, 1, "Shuffle");{
            miShuffle.setIcon(R.drawable.shuffle);
        }
        MenuItem miStop = menu.add(0, 2, 2, "Stop");{
            miStop.setIcon(R.drawable.stop);
        }
        MenuItem miSource = menu.add(0, 3, 3, "Source");{
            miSource.setIcon(R.drawable.source);
        }
    }

    public void click(View view){
        int id = view.getId();
        switch(id){
        case R.id.btnPlay:
            synchronized(this){
                if(isTuning){
                    isTuning = false;
                    btnPlay.setBackgroundResource(R.drawable.play);
                    track.pause();
                } else{
                    isTuning = true;
                    btnPlay.setBackgroundResource(R.drawable.pause);
                    playTrack();
                }
            }
            return;
        case R.id.btnPrevious:
            setTrack(0);
            loadTrack();
            playTrack();
            return;
        case R.id.btnNext:
            setTrack(1);
            loadTrack();
            playTrack();
            return;
        default:
            return;
        }
    }

    private void setTrack(int direction){
        if(direction == 0){
            currentTrack--;
            if(currentTrack < 0){
                currentTrack = trackNames.size()-1;
            }
        } else if(direction == 1){
            currentTrack++;
            if(currentTrack > trackNames.size()-1){
                currentTrack = 0;
            }
        }
        if(shuffle){
            int temp = random.nextInt(trackNames.size());
            while(true){
                if(temp != currentTrack){
                    currentTrack = temp;
                    break;
                }
                temp++;
                if(temp > trackNames.size()-1){
                    temp = 0;
                }
            }
        }
    }

    //Plays the Track
    private void playTrack(){
        if(isTuning && track != null){
            track.play();
            Toast.makeText(getBaseContext(), "Playing " + trackNames.get(currentTrack).substring(0, trackNames.get(currentTrack).length()-4), Toast.LENGTH_SHORT).show();
        }
    }

    //Simply sets shuffle to isShuffle and then displays a message for confirmation
    private void setShuffle(boolean isShuffle) {
        shuffle = isShuffle;
        if(shuffle){
            Toast.makeText(getBaseContext(), "Shuffle On", Toast.LENGTH_SHORT).show();
        } else{
            Toast.makeText(getBaseContext(), "Shuffle Off", Toast.LENGTH_SHORT).show();
        }
    }

}

3 个答案:

答案 0 :(得分:0)

如果在Eclipse上,请尝试右键单击您的项目 - &gt;来源 - &gt;清理

答案 1 :(得分:0)

  1. 在Eclipse中删除项目。
  2. 确保您的Android SDK路径已在Eclipse中设置。
  3. 在Eclipse中,单击文件 - &gt;导入 - &gt; Android-&gt;现有Android代码进入工作区,然后单击确定。
  4. 在文件夹gen中,删除应自动重建的R.java。 [可选]

答案 2 :(得分:0)

有时候我会面临eclipse的这个问题,尝试使用Intelijj Idea


快乐编码