I would to implement action bar in my project. I have implemented all inside xml,main activity etc but when I run my app action bar not appear. I'm tring to use Action bar to go in another activity.java
Could anyone help me? what's wrong in my code?
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);
}
}
I don't have errors compiling and running the app, but Action bar not appear and I don't know if the problem is inside my main_activity or in other part.
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>
Here my style:
<style name="Theme.AppCompat" parent="Base.Theme.AppCompat"/>
Edit
Modifying code after Tepits suggestion I'm running into errors with styles "@style/AppTheme.AppBarOverlay" and "@style/AppTheme.PopupOverlay" with error: "Validates resource references inside Android XML files.
<?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>
Errors:
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:5 11) 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)
Add toolbar to your 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>
Then on your onCreate
from your Activity set this toolbar
as your Toolbar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Update
Edit your styles.xml
to this:
<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>
Then on your manifest, simply add android:theme="@style/AppTheme"
to the activity
you want to have an actionbar, or you can set it on the application
tag if you want all the activities can have an actionbar.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.