播放视频冲突VideoView

时间:2015-10-06 09:36:12

标签: android

我正在做一个可以播放视频的应用程序,但是当我运行它时,它会发生冲突和logcat“java.lang.IllegalStateException:你需要使用一个Theme.AppCompat主题(或后代)与这个活动”

我的代码怎么办? 谢谢

public class Video extends AppCompatActivity {
VideoView myVideoView;
String localFilePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    localFilePath = "android.resource://" + getPackageName() + "/" + R.raw.test;
    VideoView myVideoView = (VideoView)findViewById(R.id.video);
    myVideoView.setVideoURI(Uri.parse(localFilePath));
    myVideoView.setMediaController(new MediaController(this));
    myVideoView.start();

}

1 个答案:

答案 0 :(得分:1)

您遇到此问题的原因是您尝试应用对话框主题的活动正在扩展ActionBarActivity,这需要应用AppCompat主题。

在style.xml中更改你的主题..

 <!--
         Base application theme, dependent on API level. This theme is replaced
         by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
     -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

在清单中使用它。

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:theme="@style/AppTheme">

使用Toolbar。将此代码放在xml的顶部。

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_height"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


    </android.support.v7.widget.Toolbar>

    <View
        android:id="@+id/shadow"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@drawable/shadow" />