API 17: панель действий реализована, но не отображается - Android Studio - PullRequest
0 голосов
/ 13 сентября 2018

Я хотел бы реализовать панель действий в моем проекте. Я реализовал все внутри 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>

activitymain

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" с ошибкой: "Проверяет ссылки на ресурсы внутри XML-файлов Android.

<?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 / AndroidRuntime: ИСКЛЮЧИТЕЛЬНОЕ ИСКЛЮЧЕНИЕ: основное 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)at dalvik.system.NativeStart.main (собственный метод)

1 Ответ

0 голосов
/ 13 сентября 2018

Добавьте панель инструментов к вашему 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, если вы хотите, чтобы все действия могли иметь панель действий.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...