R.java 导致的错误

发布于 2024-11-30 10:18:13 字数 6313 浏览 4 评论 0原文

我是 Android 编程新手,作为学习的一部分,我尝试运行这个开源项目(如下),但最终在 import com.example.android.apis.R; 处出现错误。还有 R 的所在。据我所知,R.java是自动生成的,我们不需要创建它或编辑它。出现这个错误的原因是什么。谁能解释一下这一点。我使用 Eclipse 来运行这个项目。

package com.example.android.apis.media;

import com.example.android.apis.R;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;


public class MediaPlayerDemo_Video extends Activity implements
   OnBufferingUpdateListener, OnCompletionListener,
   OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

private static final String TAG = "MediaPlayerDemo";
private int mVideoWidth;
private int mVideoHeight;
private MediaPlayer mMediaPlayer;
private SurfaceView mPreview;
private SurfaceHolder holder;
private String path;
private Bundle extras;
private static final String MEDIA = "media";
private static final int LOCAL_AUDIO = 1;
private static final int STREAM_AUDIO = 2;
private static final int RESOURCES_AUDIO = 3;
private static final int LOCAL_VIDEO = 4;
private static final int STREAM_VIDEO = 5;
private boolean mIsVideoSizeKnown = false;
private boolean mIsVideoReadyToBePlayed = false;

/**
*
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
   super.onCreate(icicle);
   setContentView(R.layout.mediaplayer_2);
   mPreview = (SurfaceView) findViewById(R.id.surface);
   holder = mPreview.getHolder();
   holder.addCallback(this);
   holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
   extras = getIntent().getExtras();

}

private void playVideo(Integer Media) {
   doCleanUp();
   try {

       switch (Media) {
           case LOCAL_VIDEO:
               /*
                * TODO: Set the path variable to a local media file path.
                */
               path = "";
               if (path == "") {
                   // Tell the user to provide a media file URL.
                   Toast
                           .makeText(
                                   MediaPlayerDemo_Video.this,
                                   "Please edit
MediaPlayerDemo_Video Activity, "
                                           + "and set the path
variable to your media file path."
                                           + " Your media file
must be stored on sdcard.",
                                   Toast.LENGTH_LONG).show();

               }
               break;
           case STREAM_VIDEO:
               /*
                * TODO: Set path variable to progressive streamable mp4 or
                * 3gpp format URL. Http protocol should be used.
                * Mediaplayer can only play "progressive streamable
                * contents" which basically means: 1. the movie atom has to
                * precede all the media data atoms. 2. The clip has to be
                * reasonably interleaved.
                *
                */
               path = "";
               if (path == "") {
                   // Tell the user to provide a media file URL.
                   Toast
                           .makeText(
                                   MediaPlayerDemo_Video.this,
                                   "Please edit
MediaPlayerDemo_Video Activity,"
                                           + " and set the path
variable to your media file URL.",
                                   Toast.LENGTH_LONG).show();

               }

               break;


       }

       // Create a new media player and set the listeners
       mMediaPlayer = new MediaPlayer();
       mMediaPlayer.setDataSource(path);
       mMediaPlayer.setDisplay(holder);
       mMediaPlayer.prepare();
       mMediaPlayer.setOnBufferingUpdateListener(this);
       mMediaPlayer.setOnCompletionListener(this);
       mMediaPlayer.setOnPreparedListener(this);
       mMediaPlayer.setOnVideoSizeChangedListener(this);
       mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


   } catch (Exception e) {
       Log.e(TAG, "error: " + e.getMessage(), e);
   }
}

public void onBufferingUpdate(MediaPlayer arg0, int percent) {
   Log.d(TAG, "onBufferingUpdate percent:" + percent);

}

public void onCompletion(MediaPlayer arg0) {
   Log.d(TAG, "onCompletion called");
}

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
   Log.v(TAG, "onVideoSizeChanged called");
   if (width == 0 || height == 0) {
       Log.e(TAG, "invalid video width(" + width + ") or height("
+ height + ")");
       return;
   }
   mIsVideoSizeKnown = true;
   mVideoWidth = width;
   mVideoHeight = height;
   if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
       startVideoPlayback();
   }
}

public void onPrepared(MediaPlayer mediaplayer) {
   Log.d(TAG, "onPrepared called");
   mIsVideoReadyToBePlayed = true;
   if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
       startVideoPlayback();
   }
}

public void surfaceChanged(SurfaceHolder surfaceholder, int i, int
j, int k) {
   Log.d(TAG, "surfaceChanged called");

}

public void surfaceDestroyed(SurfaceHolder surfaceholder) {
   Log.d(TAG, "surfaceDestroyed called");
}


public void surfaceCreated(SurfaceHolder holder) {
   Log.d(TAG, "surfaceCreated called");
   playVideo(extras.getInt(MEDIA));


}

@Override
protected void onPause() {
   super.onPause();
   releaseMediaPlayer();
   doCleanUp();
}

@Override
protected void onDestroy() {
   super.onDestroy();
   releaseMediaPlayer();
   doCleanUp();
}

private void releaseMediaPlayer() {
   if (mMediaPlayer != null) {
       mMediaPlayer.release();
       mMediaPlayer = null;
   }
} 

private void doCleanUp() {
   mVideoWidth = 0;
   mVideoHeight = 0;
   mIsVideoReadyToBePlayed = false;
   mIsVideoSizeKnown = false;
}

private void startVideoPlayback() {
    Log.v(TAG, "startVideoPlayback");
   holder.setFixedSize(mVideoWidth, mVideoHeight);
   mMediaPlayer.start();
}
}

这是我尝试运行的代码。

I'm new to Android programming and as a part of learning i tried to run this open source project(below) which ends up in an error at the import com.example.android.apis.R;. Also where and all there is R. As far as i know R.java is automatically generated and we don't need to create it or edit it. What is the reason for this error. Can anyone please explain this. I used Eclipse to run this project.

package com.example.android.apis.media;

import com.example.android.apis.R;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;


public class MediaPlayerDemo_Video extends Activity implements
   OnBufferingUpdateListener, OnCompletionListener,
   OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

private static final String TAG = "MediaPlayerDemo";
private int mVideoWidth;
private int mVideoHeight;
private MediaPlayer mMediaPlayer;
private SurfaceView mPreview;
private SurfaceHolder holder;
private String path;
private Bundle extras;
private static final String MEDIA = "media";
private static final int LOCAL_AUDIO = 1;
private static final int STREAM_AUDIO = 2;
private static final int RESOURCES_AUDIO = 3;
private static final int LOCAL_VIDEO = 4;
private static final int STREAM_VIDEO = 5;
private boolean mIsVideoSizeKnown = false;
private boolean mIsVideoReadyToBePlayed = false;

/**
*
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
   super.onCreate(icicle);
   setContentView(R.layout.mediaplayer_2);
   mPreview = (SurfaceView) findViewById(R.id.surface);
   holder = mPreview.getHolder();
   holder.addCallback(this);
   holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
   extras = getIntent().getExtras();

}

private void playVideo(Integer Media) {
   doCleanUp();
   try {

       switch (Media) {
           case LOCAL_VIDEO:
               /*
                * TODO: Set the path variable to a local media file path.
                */
               path = "";
               if (path == "") {
                   // Tell the user to provide a media file URL.
                   Toast
                           .makeText(
                                   MediaPlayerDemo_Video.this,
                                   "Please edit
MediaPlayerDemo_Video Activity, "
                                           + "and set the path
variable to your media file path."
                                           + " Your media file
must be stored on sdcard.",
                                   Toast.LENGTH_LONG).show();

               }
               break;
           case STREAM_VIDEO:
               /*
                * TODO: Set path variable to progressive streamable mp4 or
                * 3gpp format URL. Http protocol should be used.
                * Mediaplayer can only play "progressive streamable
                * contents" which basically means: 1. the movie atom has to
                * precede all the media data atoms. 2. The clip has to be
                * reasonably interleaved.
                *
                */
               path = "";
               if (path == "") {
                   // Tell the user to provide a media file URL.
                   Toast
                           .makeText(
                                   MediaPlayerDemo_Video.this,
                                   "Please edit
MediaPlayerDemo_Video Activity,"
                                           + " and set the path
variable to your media file URL.",
                                   Toast.LENGTH_LONG).show();

               }

               break;


       }

       // Create a new media player and set the listeners
       mMediaPlayer = new MediaPlayer();
       mMediaPlayer.setDataSource(path);
       mMediaPlayer.setDisplay(holder);
       mMediaPlayer.prepare();
       mMediaPlayer.setOnBufferingUpdateListener(this);
       mMediaPlayer.setOnCompletionListener(this);
       mMediaPlayer.setOnPreparedListener(this);
       mMediaPlayer.setOnVideoSizeChangedListener(this);
       mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


   } catch (Exception e) {
       Log.e(TAG, "error: " + e.getMessage(), e);
   }
}

public void onBufferingUpdate(MediaPlayer arg0, int percent) {
   Log.d(TAG, "onBufferingUpdate percent:" + percent);

}

public void onCompletion(MediaPlayer arg0) {
   Log.d(TAG, "onCompletion called");
}

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
   Log.v(TAG, "onVideoSizeChanged called");
   if (width == 0 || height == 0) {
       Log.e(TAG, "invalid video width(" + width + ") or height("
+ height + ")");
       return;
   }
   mIsVideoSizeKnown = true;
   mVideoWidth = width;
   mVideoHeight = height;
   if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
       startVideoPlayback();
   }
}

public void onPrepared(MediaPlayer mediaplayer) {
   Log.d(TAG, "onPrepared called");
   mIsVideoReadyToBePlayed = true;
   if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
       startVideoPlayback();
   }
}

public void surfaceChanged(SurfaceHolder surfaceholder, int i, int
j, int k) {
   Log.d(TAG, "surfaceChanged called");

}

public void surfaceDestroyed(SurfaceHolder surfaceholder) {
   Log.d(TAG, "surfaceDestroyed called");
}


public void surfaceCreated(SurfaceHolder holder) {
   Log.d(TAG, "surfaceCreated called");
   playVideo(extras.getInt(MEDIA));


}

@Override
protected void onPause() {
   super.onPause();
   releaseMediaPlayer();
   doCleanUp();
}

@Override
protected void onDestroy() {
   super.onDestroy();
   releaseMediaPlayer();
   doCleanUp();
}

private void releaseMediaPlayer() {
   if (mMediaPlayer != null) {
       mMediaPlayer.release();
       mMediaPlayer = null;
   }
} 

private void doCleanUp() {
   mVideoWidth = 0;
   mVideoHeight = 0;
   mIsVideoReadyToBePlayed = false;
   mIsVideoSizeKnown = false;
}

private void startVideoPlayback() {
    Log.v(TAG, "startVideoPlayback");
   holder.setFixedSize(mVideoWidth, mVideoHeight);
   mMediaPlayer.start();
}
}

This is the code i tried to run.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

段念尘 2024-12-07 10:18:13

我通过在 Project Property/*Android* 中将项目构建目标从 Android 1.6 更改为 Android 4.2 解决了这个问题。

旧版本不支持布局 xml 文件中的一些新语法,但这些错误信息不会显示在源代码中,而是显示在输出窗口中。

I fixed this problem by changing the project build target from Android 1.6 to Android 4.2 in Project Property/*Android*.

Some new syntax in layout xml files are not supported in old version but these error information is not display in the source code but in the output window.

雾里花 2024-12-07 10:18:13

这可能有多种原因。

确保所有文件中没有任何 XML 错误。当您出现 XML 错误时,R. 文件将不会构建。修复错误后,R. 文件应该会自动构建。如果没有,请尝试清理您的项目。

没有这一行: 。

import android.R;

另外,请确保您的班级中

This can have several causes.

Make sure that you don't have any XML errors in all your files. The R. file will not be built when you have XML errors. After you fixed the errors the R. file should be built automatically. If not, try cleaning your project.

Also, make sure you do not have this line:

import android.R;

in your class.

没︽人懂的悲伤 2024-12-07 10:18:13

这意味着布局或清单文件(XML)上存在错误。

我认为常见的错误可能是布局设计错误,缺少标签等,

请先修复它们并重新清理项目

That implies errors on the layout or Manifest file , which are XML .

I think the common mistakes can be the error of layout design , missing tags , and so on

please Fix them first and clean the project again

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文