API 17:操作栏已实现但未显示-Android Studio

时间:2018-09-13 08:17:39

标签: javascript java android xml android-studio

我要在我的项目中实施操作栏。我已经在XML,主要活动等内部实现了所有功能,但是当我运行我的应用程序操作栏时,却没有出现。 我正在尝试使用操作栏进行其他活动。java

有人可以帮助我吗?我的代码有什么问题?

xml main_menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/setting"
        android:title="@string/GotoResult"

        app:showAsAction="never">

    </item>

</menu>

Android_manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.gms.samples.vision.face.rPPG"
    android:installLocation="auto"
    android:versionCode="1"
    android:versionName="1">

    <uses-feature android:name="android.hardware.camera" />

    <uses-permission android:name="android.permission.CAMERA" />

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="23" />

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/Theme.AppCompat"
        android:label="rPPG">

        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

        <meta-data
            android:name="com.google.android.gms.vision.DEPENDENCIES"
            android:value="face" />

        <activity
            android:name="com.google.android.gms.samples.vision.face.rPPG.SplashScreen"
            android:label="@string/Splash"
            android:theme="@style/Theme.AppCompat"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!-- Main activity -->
        </activity>
        <activity android:name=".StartVitalSigns"
            android:configChanges="orientation"
            android:screenOrientation="portrait"/>
        <activity
            android:name=".RespirationResult"
            android:configChanges="orientation"
            android:screenOrientation="portrait" />
        <activity
            android:name="com.google.android.gms.samples.vision.face.rPPG.MainActivityrPPG"
            android:label="@string/rPPG"
            android:screenOrientation="portrait">
        </activity>

    </application>

</manifest>

活动主体

public final class MainActivityrPPG extends AppCompatActivity {
    private static final String TAG = "rPPG";

/**
 * Initializes the UI and initiates the creation of a face detector.
 */
@SuppressLint("ClickableViewAccessibility")
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
}
  //mycode
  //mycode
  //...
  //mycode
  //mycode


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id= item.getItemId();
        if (id == R.id.setting) {
            Intent newintent = new Intent(MainActivityrPPG.this, RespirationResult.class);
            startActivity(newintent);
            return false;

        }


        return super.onOptionsItemSelected(item);
    }
}

我没有编译和运行该应用程序的错误,但是没有显示操作栏,也不知道问题是在main_activity内部还是在其他部分。

main_activity xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:orientation="vertical">


    <com.google.android.gms.samples.vision.face.rPPG.ui.camera.CameraSourcePreview
        android:id="@+id/preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >



        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/no_faces_found"
            android:textStyle="bold"
            android:visibility="invisible" />
        <TextView
            android:id="@+id/info3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center_vertical|center_horizontal"
            android:layout_weight="1"
            android:background="#00ffffff"
            android:text=""/>




        <com.google.android.gms.samples.vision.face.rPPG.ui.camera.GraphicOverlay
            android:id="@+id/faceOverlay"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </com.google.android.gms.samples.vision.face.rPPG.ui.camera.CameraSourcePreview>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="#000000"
        android:orientation="vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">
        <com.jjoe64.graphview.GraphView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/graph"/>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_weight="2"
            android:orientation="horizontal">


            <Button
                android:id="@+id/faces"
                android:layout_width="54dp"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:theme="@style/MyButton"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:background="@drawable/bordismussati"
                android:text="@string/number_of_faces" />

            <Button
                android:id="@+id/distance"
                android:layout_width="53dp"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:theme="@style/MyButton"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:background="@drawable/bordismussati"
                android:text="@string/distance" />

            <Button
                android:id="@+id/display"
                android:layout_width="7dp"
                android:layout_height="match_parent"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@+id/faces"
                android:layout_margin="8dp"
                android:layout_weight="2"
                android:theme="@style/MyButton"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:background="@drawable/bordismussati"
                android:text="@string/display_info" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/info"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:background="@drawable/bordismussati"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/_0" />

            <TextView
                android:id="@+id/info2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:background="@drawable/bordismussati"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/no_faces_found" />




        </LinearLayout>


    </LinearLayout>



</RelativeLayout>

这是我的风格:

<style name="Theme.AppCompat" parent="Base.Theme.AppCompat"/>

修改

在Tepits建议之后修改代码,我遇到样式为“ @ style / AppTheme.AppBarOverlay”和“ @ style / AppTheme.PopupOverlay”的错误,错误为:“验证Android XML文件中的资源引用。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:orientation="vertical">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>


    <com.google.android.gms.samples.vision.face.rPPG.ui.camera.CameraSourcePreview
        android:id="@+id/preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >



        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/no_faces_found"
            android:textStyle="bold"
            android:visibility="invisible" />
        <TextView
            android:id="@+id/info3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center_vertical|center_horizontal"
            android:layout_weight="1"
            android:background="#00ffffff"
            android:text=""/>




        <com.google.android.gms.samples.vision.face.rPPG.ui.camera.GraphicOverlay
            android:id="@+id/faceOverlay"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </com.google.android.gms.samples.vision.face.rPPG.ui.camera.CameraSourcePreview>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="#000000"
        android:orientation="vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">
        <com.jjoe64.graphview.GraphView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/graph"/>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_weight="2"
            android:orientation="horizontal">


            <Button
                android:id="@+id/faces"
                android:layout_width="54dp"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:theme="@style/MyButton"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:background="@drawable/bordismussati"
                android:text="@string/number_of_faces" />

            <Button
                android:id="@+id/distance"
                android:layout_width="53dp"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:theme="@style/MyButton"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:background="@drawable/bordismussati"
                android:text="@string/distance" />

            <Button
                android:id="@+id/display"
                android:layout_width="7dp"
                android:layout_height="match_parent"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@+id/faces"
                android:layout_margin="8dp"
                android:layout_weight="2"
                android:theme="@style/MyButton"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:background="@drawable/bordismussati"
                android:text="@string/display_info" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/info"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:background="@drawable/bordismussati"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/_0" />

            <TextView
                android:id="@+id/info2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_margin="8dp"
                android:layout_weight="1"
                android:background="@drawable/bordismussati"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/no_faces_found" />




        </LinearLayout>


    </LinearLayout>



</RelativeLayout>

错误:

  

09-13 11:09:54.384 2892-2892 / com.google.android.gms.samples.vision.face.facetracker E / Android运行时:致命异常:主要       java.lang.RuntimeException:无法启动活动ComponentInfo {com.google.android.gms.samples.vision.face.facetracker / com.google.android.gms.samples.vision.face.rPPG.MainActivityrPPG}:java.lang .IllegalStateException:此活动已经具有窗口装饰提供的操作栏。请勿请求Window.FEATURE_SUPPORT_ACTION_BAR并将主题中的windowActionBar设置为false来使用工具栏。           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)           在android.app.ActivityThread.access $ 700(ActivityThread.java:157)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1289)           在android.os.Handler.dispatchMessage(Handler.java:99)           在android.os.Looper.loop(Looper.java:176)           在android.app.ActivityThread.main(ActivityThread.java:5319)           在java.lang.reflect.Method.invokeNative(本机方法)           在java.lang.reflect.Method.invoke(Method.java:511)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)           在dalvik.system.NativeStart.main(本机方法)        原因:java.lang.IllegalStateException:此活动已经具有窗口装饰提供的操作栏。请勿请求Window.FEATURE_SUPPORT_ACTION_BAR并将主题中的windowActionBar设置为false来使用工具栏。           在android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:199)           在android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)           在com.google.android.gms.samples.vision.face.rPPG.MainActivityrPPG.onCreate(MainActivityrPPG.java:163)           在android.app.Activity.performCreate(Activity.java:5326)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)           在android.app.ActivityThread.access $ 700(ActivityThread.java:157)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1289)           在android.os.Handler.dispatchMessage(Handler.java:99)           在android.os.Looper.loop(Looper.java:176)           在android.app.ActivityThread.main(ActivityThread.java:5319)           在java.lang.reflect.Method.invokeNative(本机方法)           在java.lang.reflect.Method.invoke(Method.java:511)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)           在dalvik.system.NativeStart.main(本机方法)

1 个答案:

答案 0 :(得分:1)

将工具栏添加到您的main_activity.xml

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

然后在“活动”的onCreate上将此toolbar设置为工具栏。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

更新

为此编辑您的styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

然后在清单上,只需将android:theme="@style/AppTheme"添加到您要具有操作栏的activity上,或者如果您希望所有活动都可以在application标签上进行设置操作栏。