why null value display in actionbar in android? - android

I am making simple demo of tab view .But on first line i am getting null pointer exception . I am getting null value from this method getActionBar() why ?
I will share you my class
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar=getActionBar(); /// <- this is the line causing issue
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
// ---------- methods ---------------//
}
I get null value on the line actionBar=getActionBar();
here is my manifest file and style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
</resources>
app.grudle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.naveen.tabviewswipe"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
}

Your MainActivity needs to extend AppCompatActivity (instead of FragmentActivity). Also, you'll need to use a different method to get the ActionBar:
actionBar = getSupportActionBar()
This is because you're using the support library.

if you want to extend FragmentActivity, then you need to use non-appcompat theme i.e. Holo to get actionbar
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo">
<!-- Customize your theme here. -->
</style>

You should extends ActionBarActivity and use getSupportActionBar().

Related

By running the App on versions below 22 Changes some views?

I am developing an app with minSdkVersion 11, targetSdkVersion 23 and compile/build tool version 23. On android versions, 5 and 6 my app works fine, but installing the app on versions below 22 modifies some layouts, like buttons and text fields disappear or color changes, help me with this.
Manifest File
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.khan.bilal.salamrides2">
<application
android:allowBackup="true"
android:icon="#mipmap/cc"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
Styles file
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay"
parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay"
parent="ThemeOverlay.AppCompat.Light" />
</resources>
Build Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.khan.bilal.salamrides2"
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
}
Screen Shot of android version 22(Works fine)
Screen Shot of android version below 22(problem occurs)
When you use an element that was introduced in API 21, and you try to show it on a phone with API 11, there surely will be a problem. For the older versions, you have to differentiate between the elements that you are showing - if some element is not meant to be used in API lower than 21, then you have to provide if-else check and if the API is lower than 21, then use some simple element that you are sure was supported even in API 11.
For example, if you want to use TextInputLayout, you surely can, as the library supports API 22+, but for the lower versions you should probably input text by adding classic EditText. You add both of them to your XML, hide them, and then based on user's API you show one or other.
Also, read this document.
https://developer.android.com/training/basics/supporting-devices/platforms.html
Especially this should help you get an idea what and how to do it:
private void setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Good luck :)

Can not running App on android studio

when i create a new project in android studio i find the the activity class initially extends AppCompatActivity and i have to change it to extend
Activity instead.
but the issue that i am facing is, R class is not recognisable and i receive the error "Cant resolve symbol R". i posted the build.gradle
file below and it seems to me it has no errors.
also, after tryin to find out the solution, i checked all the xml files and i found that the style.xml "posted below" has error at "Theme.AppCompat.Light.DarkActionBar"
and android studio cant resolve it.
please let me know how to fix this error.
code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.xxx.xxx);//cant resolve symbol R
}
}
style.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">//cant resolve symbol Theme+cant resolve symbol AppCompat
<!-- Customize your theme here. -->
</style>
</resources>
gradle.build:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.com.bt_11"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.+'
}
You will have to change the theme to Holo Light or Holo Dark or Material theme as pasted below;
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>

Unable to see Logo for Android Actionbar

I have been trying to get action bar icon and for some reason it is not coming at all.
Does the size matters when displaying any icon for action bar?
Mine is: 64*64 px.
My Application Manifest looks like this:
<application
android:allowBackup="true"
android:logo="#mipmap/icon_welcome_logo"
android:icon="#mipmap/icon_welcome_logo"
android:label="#string/app_name"
android:theme="#style/CustomActionBarTheme"
>
I have even tried to set the same through coding, as well as custom style.
Please advise if I am doing anything wrong??
public class MainActivity extends ActionBarActivity {
public static final String TITLE = "Account Details";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle(TITLE);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
Style.xml
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="actionMenuTextColor">#color/actionbar_text</item>
</style>
<!-- general styles for the action bar -->
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:titleTextStyle">#style/TitleTextStyle</item>
<item name="android:background">#color/actionBarBackground</item>
<item name="android:icon">#mipmap/icon_welcome_logo</item>
<item name="android:logo">#mipmap/icon_welcome_logo</item>
<item name="android:displayOptions">useLogo</item>
<!-- Support library compatibility -->
<item name="titleTextStyle">#style/TitleTextStyle</item>
</style>
<!-- action bar title text -->
<style name="TitleTextStyle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/actionBarTextColor</item>
<item name="android:textSize">#dimen/actionBarTextSize</item>
</style>
</resources>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
defaultConfig {
applicationId "au.com.app"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.squareup.dagger:dagger:1.2.1'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.1'
compile 'com.fasterxml.jackson.core:jackson-core:2.5.1'
}
I believe your issue is similar to https://stackoverflow.com/a/26451433/950427.
Change:
getSupportActionBar().setTitle(TITLE);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
To:
getSupportActionBar().setTitle(TITLE);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true); // <-- added
getSupportActionBar().setIcon(R.drawable.ic_launcher);
Dependency Updates(Unrelated to question):
dependencies {
compile 'com.android.support:appcompat-v7:22.0.0' // <-- Updated
compile 'com.squareup.dagger:dagger:1.2.2' // <-- Updated
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.1' // <-- brings in core
}

using AppCompat-21 on Lollipop device crashes

Recently I upgraded my nexus 5 to Lollipop. and I create an app using AppCompat-21. The styles under values-v21 are as follo
<resources>
<style name="AppTheme" parent="android:Theme.Material.Light">
</style>
</resources>
but when I run the app, it crashes with the information:
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
here is my build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "geone.businspector"
minSdkVersion 11
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}
It runs great under the device pre-Lollipop...
I googled a lot and had no good solution. anybody met this before? ps: I'm using Android Studio 0.8.14 and I'm new to Android Studio. thx.
The error clearly states that you're using the wrong theme. The activities you're using are from the android.support package, these activities need a ThemeCompat theme, not the Theme.Material.Light, which is for activities in android.app package. Rewrite your theme like so:
<resources>
<style name="AppTheme" parent="Theme.AppCompat">
</style>
</resources>
And this error will be gone.
Under your values/styles.xml you would be using Theme.AppCompat.Light
Use the SAME parent theme for your values-v21/styles.xml as well. This will get rid of the error as it did for me.
On Lollipop devices the theme works like it actually should so no worries :)
So I had the same issue and I got soooo tired of Googling it and seeing persons say not to use the Material Theme. Not using Material Theme changes the actionbar to gray even when you set a color for it in styles.
Anyway, the fix is that the android templates created by android studio use import android.support.v7.app.ActionBarActivity; instead of import android.app.Activity; and extend ActionBarActivity instead of activity for the main class. Just change the main class to extend an Activity instead of an ActionBarActivity. Of course more changes will have to be made if you are not using a Blank activity.

Android ActionBar - style error, but AppCompat added

I'm new to Android programming. I want to use Android Studio (I have the newest one, version 0.8.2), and style my Action Bar.
The Gradle throws the following error during the build process:
Error:Error retrieving parent for item: No resource found that matches the given name '#android:style/Theme.AppCompat.Light.DarkActionBar'.
Here's my style.xml file:
<resources>
<!-- Base application theme. -->
<style
name="AppTheme"
parent="#android:style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/SocialExitActionBar</item>
</style>
<style
name="SocialExitActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#drawable/tranparent</item>
</style>
</resources>
It's the same as the Google's documentation. In every question, I found, the answer was to add the appcompat Library. But I have the appcompat library added in my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.procode.socialexit"
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
}
Here's on pic:
link
Thanks for your help!
I had to remove the #android:style/ before each parent value.

Categories

Resources