Android actionbar returns NULL

Source: Internet
Author: User

I learned about the actionbar of android4.0 today. I have encountered a very disgusting problem and must write it down.

I started the new 2.2 Project, and then I wanted to test the actionbar. I saw the document saying that it was only available after 11. I directly changed the project to Min: 14, target: 17. This is also the beginning of the hidden danger.

In the program, call the following sentence to obtain the actionbar, but the returned value is always null. Why? Tested for one afternoon, Nima...
Actionbar mactionbar = getactionbar ();

I created a new project and directly created the SDK 14 project. The returned project is not null. I was particularly puzzled. After a long comparison, I still did not find the problem. Actually, smart programmers have long thought that theme is a problem and I did not expect it at the time.

After dinner in the evening, I came back and asked my colleagues to know that it was originally my theme army. Because my project was 2.2, theme was set:
<Style name = "appbasetheme" parent = "Android: theme. Light">
The problem lies here.

So why can I create a new project? I ignored one point: there are three theme items in my new project:
1) values --> styles. xml
<! --
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
Backward-compatibility can go here.
-->

</Style>

(2) values-v11 --> styles. xml
<! --
Base application theme for API 11 +. This theme completely replaces
Appbasetheme from Res/values/styles. xml on API 11 + devices.
-->
<Style name = "appbasetheme" parent = "Android: theme. holo. Light">
<! -- API 11 theme customizations can go here. -->

</Style>

3) values-v14 --> styles. xml
<! --
Base application theme for API 14 +. This theme completely replaces
Appbasetheme from both Res/values/styles. xml and
Res/values-v11/styles. xml on API 14 + devices.
-->
<Style name = "appbasetheme" parent = "Android: theme. holo. Light. darkactionbar">
<! -- API 14 theme customizations can go here. -->
</Style>

Read it. That is to say, the second part will be used on the SDK 11 device, and the third part will be used in SDK 14, so it is normal. And my initial project was changed from 2.2, and there was no values-v11 for the two files, so no.

Baidu once looked at it and it seems that some friends on the Internet have encountered the same problem. I guess it is the same as my problem. Another friend said that this was a bug. Actually, it was not because the root cause was not found. The following friend wrote his own test experience, which is a little helpful to me.

Http://www.cnblogs.com/shortboy/archive/2013/04/18/3029029.html

This friend mentioned the following, but the background is black. I tried it, as he said.
Getwindow (). requestfeature (window. feature_action_bar );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.