尝试在 Android 上播放视频时出现 Resources$NotFoundException?
E/AndroidRuntime(5751): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.Android/com.example.Android.OpenByApiActivity}: android.content.res.Resources$NotFoundException: 资源 ID # 0x7f030001
E/AndroidRuntime(5751):位于 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime(5751):位于 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime(5751):位于 android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime(5751):位于 android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime(5751):导致:android.content.res.Resources$NotFoundException:资源 ID #0x7f030001
有谁知道这是为什么被扔?这是我的视频和 xml 代码
public class OpenByApiActivity extends Activity implements Callback {
MediaPlayer mp = new MediaPlayer();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.video);
StringBuilder log = new StringBuilder();
String[] clearLogcat = new String[] { "logcat", "-c",};
try {
Runtime.getRuntime().exec(clearLogcat);
}
catch (IOException e) {
e.printStackTrace();
}
File location = new File("/sdcard/MediaTestFiles/sample.mp4");
Uri path = Uri.fromFile(location);
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(176, 144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDisplay(surfaceHolder);
mp= MediaPlayer.create(this, path);
if(mp!=null){
mp.start();
}}}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}}
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
E/AndroidRuntime( 5751): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.Android/com.example.Android.OpenByApiActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030001
E/AndroidRuntime( 5751): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime( 5751): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 5751): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 5751): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 5751): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f030001
Does anybody know why this is being thrown? This is my code for the video and xml
public class OpenByApiActivity extends Activity implements Callback {
MediaPlayer mp = new MediaPlayer();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.video);
StringBuilder log = new StringBuilder();
String[] clearLogcat = new String[] { "logcat", "-c",};
try {
Runtime.getRuntime().exec(clearLogcat);
}
catch (IOException e) {
e.printStackTrace();
}
File location = new File("/sdcard/MediaTestFiles/sample.mp4");
Uri path = Uri.fromFile(location);
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(176, 144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDisplay(surfaceHolder);
mp= MediaPlayer.create(this, path);
if(mp!=null){
mp.start();
}}}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}}
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能试试这个并告诉我你是否仍然遇到同样的问题
我猜问题可能出在你的路径上。
更新
移动此行吗?
您可以在 onSurfaceCreated 中
can you try this and tell me if you still have the same problem
i guess the problem may be with your path.
UPDATE
can you move this line
inside the onSurfaceCreated.