0

VideoView を表示するアプリを作成しようとしましたが、その下にvideo.xmlの一部である に示すように、その下に 3 つのボタンがありRelative Layoutます。

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Choice 1"
    android:onClick="choice1" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_centerHorizontal="true"
    android:text="Choice 2" 
    android:onClick="choice2"/>

<Button
    android:id="@+id/button3"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Choice 3" />

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="choice3" />

次に、メソッドVideoActivity.javaの下に設定されている に次のソース コードがあります。onCreate

View video = (VideoView) findViewById(R.id.videoView1);
    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(video);
    ((VideoView) video).setMediaController(mediaController);
    video.setKeepScreenOn(true);
     Uri video1 = Uri.parse("android.resource://" + getPackageName() + "/"
                + R.raw.start);
              VideoView videoHolder = null;
            videoHolder.setVideoURI(video1);
            ((VideoView) video).start();
            video.requestFocus();

LogCatレポートはこちら

I/ActivityManager( 149): Starting: Intent {cmp=com.apw.games.rpg.medieval.silver/.VideoActivity } from pid 2408
I/WindowManager( 149): Setting rotation to 1,
animFlags=1
I/ActivityManager( 149): Config changed: {scale=1.0 imsi 310/4 loc=en US touch=3 keys=2/1/1 nav=2/1 orien=2 layout-18 uiMode=17 seq=37}
DQuickSettings( 239): setGPSButton()
W/dalvikvm( 2408): threadid=1: thread exiting with uncaught exception (group=0x40018560_
E/AndroidRuntime( 2408): FATAL_EXCEPTION: main
E/AndroidRuntime( 2408): java.lang.RuntimeException:
Unable to start activity      ComponentInfo{com.apw.games.rpg.medieval.silver/com.apw.games.rpg.medieval.silver.VideoActivity}: java.lang.NullPointerException
E/AndroidRuntime( 2408): at Android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
E/ANdroidRuntime( 2408): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
E/AndroidRuntime( 2408): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 2408): at android.app.ActivityThread$H.handle.Message(ActivityThread.java931)
E/AndroidRuntime( 2408): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2408): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 2408): at android.app.ActivityThread.main(ActivityThread.java:3694)
E/AndroidRuntime( 2408): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2408): at java.lang.reflect.Method.invoke(Method.java.507)
E/AndroidRuntime( 2408): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
E/AndroidRuntime( 2408): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
E/AndroudRuntime( 2408): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2408): Caused by java.lang.NullPointerException
E/AndroidRuntime( 2408): at com.apw.games.rpg.medieval.silver.VideoActivity.onCreate(VideoActivity.java)
E/AndroidRuntime( 2408): at android.app.Intrumentation.callActivityOnCreate(Instrumentation.java.1047)
E/AndroidRuntime( 2408): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
4

1 に答える 1

1

もう少し正確に言うと...次のコードは機能する可能性がありますが、まったくテストされていません。うまくいかない場合は、次のスレッドを参照してください: How to play videos in android from assets folder or raw folder?

VideoView video = (VideoView) findViewById(R.id.videoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
video.setKeepScreenOn(true);
Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.start);
video.setVideoURI(videoUri);
video.start();

また...

VideoView videoHolder = null;
            videoHolder.setVideoURI(video1);`

nullクラッシュの理由は、このポイントである必要がある videoHolder を使用しているため、これらの 2 行である必要がありますNullPointerException

video一般に、フィールドのタイプを に変更するVideoView必要があるため、使用する場所にキャストする必要はありませんVideoView

于 2013-03-23T22:37:40.023 に答える