This question already has answers here:
R cannot be resolved - Android error

(108个回答)


4年前关闭。




This is my xml code R file is not generating please tell me where is the error and why R file is not generating

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#333333"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
    android:id="@+id/SongName"
    android:layout_width="wrap_content"
    android:text="@string/songName"
    android:layout_height="wrap_content"/>


<ImageView
    android:id="@+id/s_mp3Image"
    android:layout_width="match_parent"
    android:layout_height="200dp"
     android:padding="30dp"
     android:contentDescription="@string/music"
    android:src="@drawable/music"
    android:background="#ffffff"
    android:layout_margin="30dp" />

<TextView
   android:id="@+id/songDuration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/songDuration"
    android:layout_gravity="center"/>

<SeekBar
    android:id="@+id/s_seekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="30dp"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/s_media_rew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:onClick="rewind"
        android:contentDescription="@string/ic_media_rew"
        android:src="@android:drawable/ic_media_rew" />

    <ImageButton
        android:id="@+id/s_media_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:onClick="pause"
        android:contentDescription="@string/media_pause"
        android:src="@android:drawable/ic_media_pause" />

    <ImageButton
        android:id="@+id/s_media_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:onClick="play"
        android:contentDescription="@string/ic_media_play"
        android:src="@android:drawable/ic_media_play" />

    <ImageButton
        android:id="@+id/s_media_ff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:onClick="forward"
        android:contentDescription="@string/ic_media_ff"
        android:src="@android:drawable/ic_media_ff" />
</LinearLayout>




这是我的java代码

package com.example.androidmediaplayer;


import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

public class  MainActivity extends Activity {

private MediaPlayer mediaPlayer;
public TextView songName, duration;
private double timeElapsed = 0, finalTime = 0;
private int forwardTime = 2000, backwardTime = 2000;
private Handler durationHandler = new Handler();
private SeekBar seekbar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //set the layout of the Activity
    setContentView(R.layout.activity_list_item);   //here R file has problem

    //initialize views
    initializeViews();
}

public void initializeViews(){
    songName=(TextView)findViewById(R.id.SongName);    //here R file has problem
    mediaPlayer = MediaPlayer.create(this, R.raw.sample);  //here
       finalTime = mediaPlayer.getDuration();
       duration = (TextView) findViewById(R.id.songDuration);  //here
       seekbar = (SeekBar) findViewById(R.id.s_seekBar); //here
       songName.setText("Sample_Song.mp3");
                 seekbar.setMax((int) finalTime);
    seekbar.setClickable(false);
}

// play mp3 song
public void play(View view) {
    mediaPlayer = new MediaPlayer();
    mediaPlayer.start();
    timeElapsed = mediaPlayer.getCurrentPosition();
    seekbar.setProgress((int) timeElapsed);
    durationHandler.postDelayed(updateSeekBarTime, 100);
}

//handler to change seekBarTime
@SuppressLint("NewApi")
private Runnable updateSeekBarTime = new Runnable() {

    @SuppressLint("NewApi")
    public void run() {
        //get current position
        timeElapsed = mediaPlayer.getCurrentPosition();
        //set seekBar progress
        seekbar.setProgress((int) timeElapsed);
        //set time remaining
        double timeRemaining = finalTime - timeElapsed;
                    duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));


        //repeat yourself that again in 100 milliseconds
        durationHandler.postDelayed(this, 100);
    }
};

// pause mp3 song
public void pause(View view) {
    mediaPlayer.pause();
}

// go forward at forwardTime seconds
public void forward(View view) {
    //check if we can go forward at forwardTime seconds before song
    if ((timeElapsed + forwardTime) <= finalTime) {
        timeElapsed = timeElapsed + forwardTime;

        //seek to the exact second of the track
        mediaPlayer.seekTo((int) timeElapsed);
    }
}

// go backwards at backwardTime seconds
public void rewind(View view) {
    //check if we can go back at backwardTime seconds after song starts
    if ((timeElapsed - backwardTime) > 0) {
        timeElapsed = timeElapsed - backwardTime;

        //seek to the exact second of the track
        mediaPlayer.seekTo((int) timeElapsed);
    }
}


}

这是我的代码R文件未生成,请告诉我错误在哪里以及为什么R文件未生成
这是我的xml代码R文件未生成,请告诉我错误在哪里,为什么未生成R文件,这是我的xml代码R文件未生成,请告诉我错误在哪里,为什么R文件未生成,这是我的xml代码R文件未生成,请告诉我错误在哪里以及为什么R文件未生成

最佳答案

您应该尝试清理您的项目!

09-30 15:05