android how change background action bar - android

Hi i would change my actionbar background color. I've found a lot of tutorial but when i launch my app it crashes giving me error message.
This is what i've done.
AndroidManifest.xml:
<activity
android:name=".UserActivity"
android:label="#string/app_name"
android:parentActivityName="com.example.securepassword.MainActivity"
android:theme="#style/MyTheme" >
<intent-filter>
<action android:name="user" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".AddItemActivity"
android:label="#string/app_name"
android:parentActivityName="com.example.securepassword.UserActivity"
android:theme="#style/MyTheme" >
<intent-filter>
<action android:name="add_item" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
UserActivity and AddItemActivity are the activities where actionbar appears.
I have created my personal style in /res/values-v11/styles.xml
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#0086D4</item>
</style>
when i start UserActivity my app crashes giving me this error:
01-10 12:53:08.337: E/AndroidRuntime(15095): FATAL EXCEPTION: main
01-10 12:53:08.337: E/AndroidRuntime(15095): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.securepassword/com.example.securepassword.UserActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.os.Looper.loop(Looper.java:137)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-10 12:53:08.337: E/AndroidRuntime(15095): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 12:53:08.337: E/AndroidRuntime(15095): at java.lang.reflect.Method.invoke(Method.java:525)
01-10 12:53:08.337: E/AndroidRuntime(15095): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-10 12:53:08.337: E/AndroidRuntime(15095): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-10 12:53:08.337: E/AndroidRuntime(15095): at dalvik.system.NativeStart.main(Native Method)
01-10 12:53:08.337: E/AndroidRuntime(15095): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
01-10 12:53:08.337: E/AndroidRuntime(15095): at com.example.securepassword.UserActivity.onCreate(UserActivity.java:17)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.Activity.performCreate(Activity.java:5133)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-10 12:53:08.337: E/AndroidRuntime(15095): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-10 12:53:08.337: E/AndroidRuntime(15095): ... 11 more
The problem appears when in androidmanifest i use MyTheme like style for both activities..what's wrong?

You have answer in exception message
You need to use a Theme.AppCompat theme (or descendant) with this activity.
Use Theme.AppCompat.
Additionally you can use toolbar to customize you action bar
You can add it in layout xml such as standard view and set it in your activity by setSupportActionBar(toolbarObject) method as is shown below
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:elevation="5dp"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<!-- your layout -->
</LinearLayout>
YourActivty.class
public class YourActivty extends ActionBarActivity {
Toolbar mToolbar;
public void onCreate(Bundle savedInstanceState) {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
// configure toolbar stuff
setSupportActionBar(mToolbar);
}
}
But when you want to use toolbar. You have to use Theme.AppCompat.NoActionBar theme

you can try using code in your Activity like this:
ActionBar ab = getActionBar();
ab.setHomeButtonEnabled(true);
ColorDrawable colorDrawable = new ColorDrawable(
Color.parseColor("#0086D4"));
ab.setBackgroundDrawable(colorDrawable);
To change the color of text on ActionBar use this:
ab.setTitle(Html.fromHtml("<font color='#YOUR_COLORCODE'>ActionBarTitle </font>"));
OR
int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
TextView title = (TextView) findViewById(actionBarTitleId);
if (title != null) {
title.setTextColor(YOUR COLOR );
}
}

Change the inheritance of your main activity to Activity (import android.app.Activity) , i think it solves the problem with using custom theme

Related

Why I can't start a new activity?

Whenever I tried to click the button it stops working.
Here are the logs:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blogspot.hlowrold.mybooks/com.blogspot.hlowrold.mybooks.Main2Activity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:356)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:325)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:286)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.blogspot.hlowrold.mybooks.Main2Activity.onCreate(Main2Activity.java:13)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
What is the problem here? Please help me.
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
example
styles.xml:
<style name="ThemeCompatLight" parent="Theme.AppCompat.Light.NoActionBar">
</style>
AndroidManifest.xml
<activity
android:name=".your.activity.name"
android:theme="#style/ThemeCompatLight" />
make your style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
you need add appcompat lib to build.gradle
compile 'com.android.support:appcompat-v7:25.3.1'
and add theme in styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"/>
and in manifest file, peresent activity this way
<activity
android:name="YOUR_ACTIVITY"
android:theme="#style/AppTheme" />

How to change action bar background and text colors

I am using navigation drawer from this tutorial
http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
and i am trying to change action bar background from this selected answer
Set title background color
but when i run my app app got crash and shows error
Manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="info.androidhive.slidingmenu.MainActivity"
android:label="#string/app_name" >
</activity>
<activity android:name="Login_Page"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
>
</activity>
<activity android:name="Register"></activity>
<activity android:name="SplashActivity"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
LogCat
07-01 12:57:38.882: E/AndroidRuntime(14690): FATAL EXCEPTION: main
07-01 12:57:38.882: E/AndroidRuntime(14690):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}:
android.util.AndroidRuntimeException: You cannot combine custom titles
with other title features 07-01 12:57:38.882: E/AndroidRuntime(14690):
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
07-01 12:57:38.882: E/AndroidRuntime(14690): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-01 12:57:38.882: E/AndroidRuntime(14690): at
android.app.ActivityThread.access$600(ActivityThread.java:128) 07-01
12:57:38.882: E/AndroidRuntime(14690): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-01 12:57:38.882: E/AndroidRuntime(14690): at
android.os.Handler.dispatchMessage(Handler.java:99) 07-01
12:57:38.882: E/AndroidRuntime(14690): at
android.os.Looper.loop(Looper.java:137) 07-01 12:57:38.882:
E/AndroidRuntime(14690): at
android.app.ActivityThread.main(ActivityThread.java:4517) 07-01
12:57:38.882: E/AndroidRuntime(14690): at
java.lang.reflect.Method.invokeNative(Native Method) 07-01
12:57:38.882: E/AndroidRuntime(14690): at
java.lang.reflect.Method.invoke(Method.java:511) 07-01 12:57:38.882:
E/AndroidRuntime(14690): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-01 12:57:38.882: E/AndroidRuntime(14690): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 07-01
12:57:38.882: E/AndroidRuntime(14690): at
dalvik.system.NativeStart.main(Native Method) 07-01 12:57:38.882:
E/AndroidRuntime(14690): Caused by:
android.util.AndroidRuntimeException: You cannot combine custom titles
with other title features 07-01 12:57:38.882: E/AndroidRuntime(14690):
at
com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:245)
07-01 12:57:38.882: E/AndroidRuntime(14690): at
com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2701)
07-01 12:57:38.882: E/AndroidRuntime(14690): at
com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2921)
07-01 12:57:38.882: E/AndroidRuntime(14690): at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
07-01 12:57:38.882: E/AndroidRuntime(14690): at
android.app.Activity.setContentView(Activity.java:1835) 07-01
12:57:38.882: E/AndroidRuntime(14690): at
info.androidhive.slidingmenu.MainActivity.onCreate(MainActivity.java:50)
07-01 12:57:38.882: E/AndroidRuntime(14690): at
android.app.Activity.performCreate(Activity.java:4470) 07-01
12:57:38.882: E/AndroidRuntime(14690): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-01 12:57:38.882: E/AndroidRuntime(14690): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-01 12:57:38.882: E/AndroidRuntime(14690): ... 11 more 07-01
12:57:38.892: E/AlarmManagerService(378):
android_server_AlarmManagerService_set to type=3, 51461.572000000
07-01 12:57:38.892: V/AlarmManager(378): waitForAlarm result :8 07-01
12:57:38.902: E/android.os.Debug(378): !#Dumpstate > dumpstate -k -t
-n -z -d -o /data/log/dumpstate_app_error
Style.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="WindowTitleBackground">
<item name="android:background">#color/titlebackgroundcolor</item>
<item name="android:textColor">#color/titletextcolor</item>
</style>
<!--
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="android:Theme.Light">
<!--
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>
</resources>
Try this in styles.xml:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
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="android:Theme.Light">
<!--
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">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#color/titlebackgroundcolor</item>
<item name="android:titleTextStyle">#style/MyActionBar.Text</item>
</style>
<style name="MyActionBar.Text" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#FFFF00</item>
</style>
</resources>
Remove these two lines:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
It will work for you.

android studio my helloWorld project has stopped

I just instilled Android Studio and when I run the sample project hello world in the emulator it gives me my project has stopped and it doesn't work
myActivity.java
package b3du.im.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MyActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
}
activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="b3du.im.myapplication" >
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
logcat error
08-01 05:13:56.557 1884-1884/b3du.im.myapplication I/Process﹕ Sending signal. PID: 1884 SIG: 9
08-01 05:21:26.244 1944-1944/b3du.im.myapplication D/AndroidRuntime﹕ Shutting down VM
08-01 05:21:26.244 1944-1944/b3du.im.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2cc9b20)
08-01 05:21:26.284 1944-1944/b3du.im.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: b3du.im.myapplication, PID: 1944
java.lang.RuntimeException: Unable to start activity ComponentInfo{b3du.im.myapplication/b3du.im.myapplication.MyActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:108)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at b3du.im.myapplication.MyActivity.onCreate(MyActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
This is because you are using ActionBarActivity, which requires the AppCompat theme to be applied.
Either use that theme, Or use simple Activity. See a similar issue
as Stated by MysticMagic change ActionBarActivity to Activity or change your theme to AppCompat
explore package got to res-> values->style.xml and change style
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
Your issue is with AppTheme that you have set currently in you app.
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:108)
You are inheriting you from ActionBarActivity so you have to use Theme.AppCompat for your application. Change app base theme in styles.xml to
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light">
If you are facing issue in loading this theme than please follow the below link to resolve compilation issue.
Can't Find Theme.AppCompat.Light for New Android ActionBar Support

Customizing title bar in Android. Getting Exception You need to use a Theme.AppCompat theme

Search a lot but nothing found useful.
I am creating a simple app with my customized Title bar but not getting so far from days. I am using this https://developer.android.com/training/basics/actionbar/styling.html.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.title"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:theme="#style/CustomActionBarTheme"//title bar
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.title.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I have created theme.xml like res/values/themes.xml.
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#CCCCCC</item>
<!-- Support library compatibility -->
</style>
</resources>
Getting Exception :(
logcat:
04-22 11:37:21.755: E/test(11652): Exception
04-22 11:37:21.757: E/AndroidRuntime(11652): FATAL EXCEPTION: main
04-22 11:37:21.757: E/AndroidRuntime(11652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.title/com.example.title.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.access$600(ActivityThread.java:156)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.os.Looper.loop(Looper.java:153)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.main(ActivityThread.java:5299)
04-22 11:37:21.757: E/AndroidRuntime(11652): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 11:37:21.757: E/AndroidRuntime(11652): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 11:37:21.757: E/AndroidRuntime(11652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-22 11:37:21.757: E/AndroidRuntime(11652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-22 11:37:21.757: E/AndroidRuntime(11652): at dalvik.system.NativeStart.main(Native Method)
04-22 11:37:21.757: E/AndroidRuntime(11652): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
04-22 11:37:21.757: E/AndroidRuntime(11652): at com.example.title.MainActivity.onCreate(MainActivity.java:18)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.Activity.performCreate(Activity.java:5182)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
04-22 11:37:21.757: E/AndroidRuntime(11652): ... 11 more
Guide me with this please
Your MainActivity probably extends from ActionBarActivity. And you are probably referencing AppCompat. So you need to use Theme.AppCompat.
Also you have android:minSdkVersion="11". So there is no need to extend ActionBarActivty and no need to reference AppCompat and no need for Theme.AppCompat
http://developer.android.com/guide/topics/ui/actionbar.html
Also check
https://developer.android.com/training/basics/actionbar/styling.html

Android ActionBar NullPointerException

I want to enable the Android ActionBar, however it doesn't work for me.
Here is my MainActivity:
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
ActionBar actionBar = getActionBar();
actionBar.show();
//more code............
and here is the logcat:
03-04 16:31:09.423: E/AndroidRuntime(1441): FATAL EXCEPTION: main
03-04 16:31:09.423: E/AndroidRuntime(1441): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.salebook/com.example.salebook.MainActivity}: java.lang.NullPointerException
03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-04 16:31:09.423: E/AndroidRuntime(1441): at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 16:31:09.423: E/AndroidRuntime(1441): at android.os.Looper.loop(Looper.java:137)
03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-04 16:31:09.423: E/AndroidRuntime(1441): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 16:31:09.423: E/AndroidRuntime(1441): at java.lang.reflect.Method.invoke(Method.java:511)
03-04 16:31:09.423: E/AndroidRuntime(1441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-04 16:31:09.423: E/AndroidRuntime(1441): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-04 16:31:09.423: E/AndroidRuntime(1441): at dalvik.system.NativeStart.main(Native Method)
03-04 16:31:09.423: E/AndroidRuntime(1441): Caused by: java.lang.NullPointerException
03-04 16:31:09.423: E/AndroidRuntime(1441): at com.example.salebook.MainActivity.onCreate(MainActivity.java:130)
03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.Activity.performCreate(Activity.java:5104)
03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-04 16:31:09.423: E/AndroidRuntime(1441): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-04 16:31:09.423: E/AndroidRuntime(1441): ... 11 more
Where line 130 is anctionBar.show() and I have in res/menu/menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/itemid_0"
android:title="Action Item 0"
android:icon="#drawable/ic_launcher"
android:orderInCategory="0"
android:showAsAction="ifRoom|withText" />
<item android:id="#+id/itemid_1"
android:title="Action Item 1"
android:orderInCategory="0" />
<item android:id="#+id/itemid_2"
android:title="Action Item 2"
android:orderInCategory="0" />
<item android:id="#+id/itemid_3"
android:title="Action Item 3"
android:orderInCategory="0" />
</menu>
I didn't find the problem. Hope someone help me.
Thanks.
Also,in the appThem style:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowActionBar">true</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
You Actionbar returns null because you don't have actionbar but you are requesting using getActionbar()
Make sure that you've window feature to show your actionbar, That is required for the actionbar to show. Check the below links for more details.
...check this question getActionBar returns null And also check this link for the more information on your problem http://blog.perpetumdesign.com/2011/08/strange-case-of-dr-action-and-mr-bar.html
You Remove
android:theme="#style/AppTheme"
in "application" from AndroidManifest.xml
Do you actually see an actionbar if you remove the line actionBar.show()?
If not, it could be related to your theme, e.g. Theme.Light.NoActionBar.
go to the AndroidManifest.xml and replace
android:theme="#style/AppTheme"
by
android:theme="#android:style/Theme.Holo.Light.DarkActionBar"

Categories

Resources