我想在我的项目中实现操作栏。我已经实现了所有内部 xml、主要 Activity 等,但是当我运行我的应用程序时,操作栏没有出现。 我正在尝试使用操作栏进入另一个 Activity.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>
Activity 主体
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/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.gms.samples.vision.face.facetracker/com.google.android.gms.samples.vision.face.rPPG.MainActivityrPPG}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309) at android.app.ActivityThread.access$700(ActivityThread.java:157) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5319) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:199) at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130) at com.google.android.gms.samples.vision.face.rPPG.MainActivityrPPG.onCreate(MainActivityrPPG.java:163) at android.app.Activity.performCreate(Activity.java:5326) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309) at android.app.ActivityThread.access$700(ActivityThread.java:157) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5319) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method)
最佳答案
将工具栏添加到您的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>
然后在 Activity 的 onCreate
上将此工具栏
设置为工具栏。
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>
然后在您的 list 上,只需将 android:theme="@style/AppTheme"
添加到您想要操作栏的 activity
中,或者您也可以将其设置为如果您希望所有 Activity 都可以有一个操作栏,则使用 application
标记。
关于javascript - API 17 : Action Bar implemented but not appear - Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52309424/