尝试在 Android 上播放视频时出现 Resources$NotFoundException?

发布于 2024-12-01 23:58:33 字数 2918 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(1

千鲤 2024-12-08 23:58:33

你能试试这个并告诉我你是否仍然遇到同样的问题

final File location = new File(Environment.getExternalStorageDirectory(), "/MediaTestFiles/sample.mp4");

我猜问题可能出在你的路径上。

更新

移动此行吗?

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    //mp.setDisplay(surfaceHolder);
    mp.setDisplay(arg0);
}

您可以在 onSurfaceCreated 中

can you try this and tell me if you still have the same problem

final File location = new File(Environment.getExternalStorageDirectory(), "/MediaTestFiles/sample.mp4");

i guess the problem may be with your path.

UPDATE

can you move this line

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    //mp.setDisplay(surfaceHolder);
    mp.setDisplay(arg0);
}

inside the onSurfaceCreated.

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