my activity extends AppCompatActivity and crash on Launch - android

I am creating one application and i am getting error,
On launching it crash and says
Caused by: java.lang.IllegalStateException: You need to use a
Theme.AppCompat theme (or descendant) with this activity.
Here is my Activity:
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
}
}, 3000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_splash_screen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#style/MyAnimation.Window</item>
<item name="android:windowBackground">#color/background</item>
</style>
<!-- Application theme. -->
<style name="AppTheme1" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
Here is my manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme1" >
<activity
android:name=".activity.SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here is complete error log:
FATAL EXCEPTION: main 10-16 15:56:26.135
25342-25342/com.social_infotech.renthouse E/AndroidRuntime:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.social_infotech.renthouse/com.social_infotech.renthouse.activity.SplashScreen}:
java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity. 10-16 15:56:26.135
25342-25342/com.social_infotech.renthouse E/AndroidRuntime: at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at
android.app.ActivityThread.access$600(ActivityThread.java:141) 10-16
15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at
android.os.Handler.dispatchMessage(Handler.java:99) 10-16 15:56:26.135
25342-25342/com.social_infotech.renthouse E/AndroidRuntime: at
android.os.Looper.loop(Looper.java:137) 10-16 15:56:26.135
25342-25342/com.social_infotech.renthouse E/AndroidRuntime: at
android.app.ActivityThread.main(ActivityThread.java:5103) 10-16
15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native
Method) 10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at
java.lang.reflect.Method.invoke(Method.java:525) 10-16 15:56:26.135
25342-25342/com.social_infotech.renthouse E/AndroidRuntime: at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-16
15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: Caused by: java.lang.IllegalStateException: You
need to use a Theme.AppCompat theme (or descendant) with this
activity. 10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at
android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:112)
10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at
android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:148)
10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at
android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:60)
10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse
E/AndroidRuntime: at
com.social_infotech.renthouse.activity.SplashScreen.onCreate(SplashScreen.java:16)

Extends your activity with FragmentActivity or Activity
Because you are using NoAcionBar as your theme.

As I've said, something's not adding up.
You seem to be using different themes:
In the manifest you have
#style/AppTheme1
while the styles file contains
#style/AppTheme

Just copy your following code to your values-21 >>> Styles.xml
then clean your project and try to run.
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#style/MyAnimation.Window</item>
<item name="android:windowBackground">#color/background</item>
</style>
<!-- Application theme. -->
<style name="AppTheme1" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

for a noActionBar activity :
style.xml
<style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
</style>
AndroidManifest.xml
<activity
android:name=".SplashScreenActivity"
android:theme="#style/AppThemeNoActionBar" >
</activity>
SplashScreen.java
import android.support.v7.app.AppCompatActivity;
public class SplashscreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
}
}

Please refer to this thread.
EDIT: A few answer from the above thread may help you with this issue. I'll list a summary:
molokoka's answer, in order to add ActionBarCompat in your activity or application should use #style/Theme.AppCompat theme in AndroidManifest.xml like this:
<activity
...
android:theme="#style/Theme.AppCompat" />
molokoka also states that usually you need to customize your ActionBar. Once again, please follow his answer for details.
tyczj also makes a very good point:
Check and make sure that you do not have another values folder that references theme.styled and does not use AppCompat theme
ie values-v11 folder.
Side note: Sorry, I'm unable to add a comment due to low reputation. That's the reason why I placed an answer.

Related

Unable to resolve error Theme.AppCompat theme (or descendant)

I looked at other SO posts and configured accordingly. But I still see this issue on some Android devices (4.3.1). It works 75% of time.
You need to use a Theme.AppCompat theme (or descendant) with this activity.
What is wrong with code below?
BaseActivity extends android.support.v7.appAppCompatActivity
<application
android:allowBackup="true"
android:theme="#style/AppTheme" >
All activities (crashed acitivity as well) use AppTheme from application tag. One activity uses:
<activity
android:name=".Activities.EntryActivity"
android:noHistory="true"
android:theme="#style/SplashTheme" >
I don't have any other style.xml.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
From Android:
<style name="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
java.lang.RuntimeException: Unable to start activity
ComponentInfo{Activities.MyAcitivity}:
java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity. 1 at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
2 at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
3 at android.app.ActivityThread.access$600(ActivityThread.java:141)
4 at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
5 at android.os.Handler.dispatchMessage(Handler.java:99) 6 at
android.os.Looper.loop(Looper.java:137) 7 at
android.app.ActivityThread.main(ActivityThread.java:5103) 8 at
java.lang.reflect.Method.invokeNative(Native Method) 9 at
java.lang.reflect.Method.invoke(Method.java:525) 10 at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12 at com.android.internal.os.ZygoteInit.main(Native Method) 13 at
dalvik.system.NativeStart.main(Native Method) 14 Caused by:
java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity. 15 at
android.support.v7.app.AppCompatDelegateImplV7.i(SourceFile:340) 16 at
android.support.v7.app.AppCompatDelegateImplV7.h(SourceFile:309) 17 at
android.support.v7.app.AppCompatDelegateImplV7.setContentView(SourceFile:273)
18 at
android.support.v7.app.AppCompatActivity.setContentView(SourceFile:136)
19 at .Commons.BaseActivity.onCreate(SourceFile:236) 20 at
.MyActivity.onCreate(SourceFile:24) 21 at
android.app.Activity.performCreate(Activity.java:5133) 22 at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
23 at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
24 ... 12 more 25 java.lang.IllegalStateException: You need to use a
Theme.AppCompat theme (or descendant) with this activity. 26 at
android.support.v7.app.AppCompatDelegateImplV7.i(SourceFile:340) 27 at
android.support.v7.app.AppCompatDelegateImplV7.h(SourceFile:309) 28 at
android.support.v7.app.AppCompatDelegateImplV7.setContentView(SourceFile:273)
29 at
android.support.v7.app.AppCompatActivity.setContentView(SourceFile:136)
30 at Activities.Commons.BaseActivity.onCreate(SourceFile:236) 31 at
Activities.Accounts.AppLaunchActivity.onCreate(SourceFile:24) 32 at
android.app.Activity.performCreate(Activity.java:5133) 33 at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
34 at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
35 at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
36 at android.app.ActivityThread.access$600(ActivityThread.java:141)
37 at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
38 at android.os.Handler.dispatchMessage(Handler.java:99) 39 at
android.os.Looper.loop(Looper.java:137) 40 at
android.app.ActivityThread.main(ActivityThread.java:5103) 41 at
java.lang.reflect.Method.invokeNative(Native Method) 42 at
java.lang.reflect.Method.invoke(Method.java:525) 43 at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
44 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
45 at com.android.internal.os.ZygoteInit.main(Native Method) 46 at
dalvik.system.NativeStart.main(Native Method)
try this:
in res/values/styles.xml:
<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>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Now in your manifest:
<application
android:theme="#style/AppTheme"> //base Application theme
<activity
android:name=".Activities.EntryActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"> //Activity Theme
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Android crash with translucent splash screen

I was tried to remove system white splash before actual splash screen on application start.
I follow this topic: How To fix white screen on app Start up? and created translucent style for splash activity:
<style name="Splash" parent="#android:style/Theme.Translucent">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowDisablePreview">true</item>
</style>
But at first launch after the installation, application crashed with the following unhandled exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ru.perekrestok.app, PID: 13791
java.lang.IllegalArgumentException: reportSizeConfigurations: ActivityRecord not found for: Token{5b77a38 null}
at android.os.Parcel.readException(Parcel.java:1687)
at android.os.Parcel.readException(Parcel.java:1636)
at android.app.ActivityManagerProxy.reportSizeConfigurations(ActivityManagerNative.java:6844)
at android.app.ActivityThread.reportSizeConfigurations(ActivityThread.java:2768)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2711)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
After first launch application starts normally. Can I fix this behaviour? I'm using latest android support library, if this is important
Change you activity entry in manifest with this. This will remove the white splash( as the theme is translucent) & your actual splash will be visible.
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Translucent.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Why won't my Android App open in Fullscreen?

I know that this question has been asked before, but the provided answers did not solve my case, so I'll ask for an individual solution.
My code
My AndroidManifest.xml looks like this:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
>
<activity
android:name=".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>
<activity
android:name=".KategorieAuswahl"
android:label="#string/title_activity_kategorie_auswahl" >
</activity>
<activity
android:name=".ZeigeFragen"
android:label="#string/title_activity_zeige_fragen" >
</activity>
</application>
And my styles.xml looks like this:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
I want my App to open in fullscreen and to hide the TitleBar. The first thing I did was to add a android:theme="#android:style/Theme.NoTitleBar.Fullscreen" attribute to the <application>-Tag of my AndroidManifest.xml.
This leads to the error You need to use a Theme.AppCompat theme (or descendant) with this activity. Here is the full stacktrace:
05-27 21:24:26.889 2121-2121/de.test.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.test.myapp/de.test.myapp.MainActivity}: 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.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:113)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at de.test.myapp.MainActivity.onCreate(MainActivity.java:24)
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)
There are multiple questions on stackoverflow referencing this error.
Approach #1
One solution suggests to change the Java inheritance from ActionBarActivity to Activity and leave the manifest as it is.
So I updated my the <style> element in my styles.xml and removed the DarkActionBar part:
<style name="AppTheme" parent="Theme.AppCompat.Light">
This still gives me the same error.
Approach #2
The next solution suggests to change my styles.xml's parent attribute from
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to <style name="AppTheme" parent="Theme.AppCompat">
This makes it possible to use android:theme="#style/Theme.AppCompat.NoActionBar" for my <activity>, but I don't see an option to make it fullscreen then.
Approach #3
Another solution soggests to add <item name="android:windowNoTitle">true</item> to my <style> element but this doesn't change anything at all.
Conclusion
I really tried a lot, but nothing seems to work. Anyway I have problems understanding the theme inheritance chain and when I may use .NoTitleBar and .Fullscreen.
After all I don't care which theme to use, as long I can make the app fullscreen without a title bar. Can you help me to pick the right attributes?
extend ActionBarActivity or the new one, and use Theme.AppCompat.NoActionBar, now in your Activity
if (Build.VERSION.SDK_INT < 16) {
Activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}else{
Activity.getWindow().getDecorView().
setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
}
Hope it helps..

ActionBar giving nullPointerException?

I've been trying to figure this one out for a while. I have an app that uses the action bar but every time I want to call it in comes up null. Giving this error
12-01 10:54:11.499: E/AndroidRuntime(2385): FATAL EXCEPTION: main
12-01 10:54:11.499: E/AndroidRuntime(2385): Process: com.example.jobpool, PID: 2385
12-01 10:54:11.499: E/AndroidRuntime(2385): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jobpool/com.example.jobpool.Signup}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.os.Looper.loop(Looper.java:135)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread.main(ActivityThread.java:5221)
12-01 10:54:11.499: E/AndroidRuntime(2385): at java.lang.reflect.Method.invoke(Native Method)
12-01 10:54:11.499: E/AndroidRuntime(2385): at java.lang.reflect.Method.invoke(Method.java:372)
12-01 10:54:11.499: E/AndroidRuntime(2385): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-01 10:54:11.499: E/AndroidRuntime(2385): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-01 10:54:11.499: E/AndroidRuntime(2385): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
12-01 10:54:11.499: E/AndroidRuntime(2385): at com.example.jobpool.Signup.onCreate(Signup.java:16)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.Activity.performCreate(Activity.java:5933)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-01 10:54:11.499: E/AndroidRuntime(2385): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-01 10:54:11.499: E/AndroidRuntime(2385): ... 10 more
I know this has been discussed in other threads but I haven't found a solution that works for me. I have the app.support.v7 library added.
Here's the onCreate method:
public class Signup extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
getActionBar().hide();
SetListeners();
}
....
Here's the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jobpool"
android:versionCode="1"
android:versionName="1.0" >
android:Debuggable = "True"
android:VMsafemode ="True"
android:AllowBackup = "True"
android:AllowClearuserdate= "True"
android:HardwareAccelerated = "True"
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/logo_icon_large"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<activity
android:name=".Signup"
android:label="signup" >
</activity>
<activity
android:name=".Dashboard"
android:label="dashboard" >
</activity>
<activity
android:name=".UserRegistration"
android:label="#string/title_activity_user_registration" >
</activity>
</application>
</manifest>
and lastly my values/styles file
<resources>
<!--
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="#style/Theme.AppCompat.Light.DarkActionBar">
<!--
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. -->
<item name="android:windowNoTitle">true</item> <!-- Hides the Action Bar -->
<item name="android:windowFullscreen">true</item> <!-- Hides the status bar -->
</style>
</resources>
if you are using the support library then you have to use getSupportActionBar() instead of getActionBar(), and instead of extends Activity you have to extends ActionBarActivity. Here you can find the documentation
you declare your AppTheme with the actionbar hidden:
WIth these flags, it will never be there
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowNoTitle">true</item> <!-- Hides the Action Bar -->
<item name="android:windowFullscreen">true</item> <!-- Hides the status bar -->
</style>
Your getActionBar method is not returning anything i suppose, it is null, so it is unable to call the hide method.
By the way, how are you supposed to get ActionBar reference directly using getActionBar when you haven't even extended your Activity from ActionBarActivity???
if you are using support library put the code in this way
public class Signup extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
getSupportActionBar().hide();
SetListeners();
}
....

Trouble implementing Material Theme

I am using this as a tutorial to implement the Material Theme to an existing app on pre Android 5.0 devices. My problem is that I am getting a Null Pointer Exception every-time I call getActionBar().something or getSupportActionBar().something.
Here is all I have done to implement the Material Theme.
In values/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="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#1A7E99</item>
<item name="colorPrimaryDark">#16657A</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
</style>
<!-- View pager progress indecator theme -->
<style name="StyledIndicators" parent="AppBaseTheme">
<item name="vpiCirclePageIndicatorStyle">#style/CustomCirclePageIndicator</item>
</style>
<style name="CustomCirclePageIndicator">
<item name="fillColor">#ffffff</item>
<item name ="strokeWidth">2dp</item>
<item name ="strokeColor">#cfd3d4</item>
<item name="radius">8dp</item>
<item name="centered">true</item>
</style>
<style name="Widget"></style>
<style name="Widget.FloatingHintEditText" parent="#android:style/Widget.EditText">
<item name="android:paddingTop">0dp</item>
</style>
In my values-v21/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
</resources>
As I have set the windowActionBar to false, I have a toolbar layout.
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"/>
And finally on to the MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // NULL POINTER EXCEPTION here
getSupportActionBar().setHomeButtonEnabled(true);
// rest of my code
}
}
I am adding my theme in the Manifest:
android:theme="#style/AppTheme"
My Logcat:
11-11 12:40:54.798: E/ResourceType(32738): Style contains key with bad entry: 0x01010479
11-11 12:40:55.349: E/AndroidRuntime(32738): FATAL EXCEPTION: main
11-11 12:40:55.349: E/AndroidRuntime(32738): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.driverdesignstudio.drvr/com.driverdesignstudio.drvr.MainActivity}: java.lang.NullPointerException
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread.access$600(ActivityThread.java:162)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.os.Handler.dispatchMessage(Handler.java:107)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.os.Looper.loop(Looper.java:194)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread.main(ActivityThread.java:5371)
11-11 12:40:55.349: E/AndroidRuntime(32738): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 12:40:55.349: E/AndroidRuntime(32738): at java.lang.reflect.Method.invoke(Method.java:525)
11-11 12:40:55.349: E/AndroidRuntime(32738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-11 12:40:55.349: E/AndroidRuntime(32738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-11 12:40:55.349: E/AndroidRuntime(32738): at dalvik.system.NativeStart.main(Native Method)
11-11 12:40:55.349: E/AndroidRuntime(32738): Caused by: java.lang.NullPointerException
11-11 12:40:55.349: E/AndroidRuntime(32738): at com.driverdesignstudio.drvr.MainActivity.onCreate(MainActivity.java:123)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.Activity.performCreate(Activity.java:5122)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
11-11 12:40:55.349: E/AndroidRuntime(32738): ... 11 more
My questions:
Why am I getting a Null Pointer Exception for the getActionBar() when I am adding my toolbar layout to the MainActivity?
How do I add Material design theme to my app.
Cheers,
Rakshak
UPDATE: your actual problem is that you have the <android.support.v7.widget.Toolbar code in an own toolbar.xml file. this has to be in activity_main.xml and all other layouts that you want to use with an actionbar.
The Toolbar allows more customization than the default ActionBar (mostly appearance related). If you want or need to customize the actionbar, you need to set a Toolbar as ActionBar. If not. you dont need to do that and you can simply use the default ActionBar provided by the Theme (you have to remove the windowActionBar false part from your theme).
if you want to use a toolbar, you have to ensure that there is a Toolbar view in your layouts.
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
then you can use your code.
but you should also ensure that getSupportActionBar() does not return null before you use it. in case that the layout is missing the toolbar.
OLD ANSWER BELOW
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // NULL POINTER EXCEPTION here
getSupportActionBar().setHomeButtonEnabled(true);
you only set the toolbar if the view has been found. it seems that the view has not been found.
after the if you access the actionbar that has not been set and is therefore null.
also in your them you have <item name="windowActionBar">false</item> that disables the default actionbar.
you can do following:
set windowActionbar to true in your theme then you have the default actionbar and you wont need the Toolbar.
if you want to use your toolbar on a non default location you have to ensure that findViewById(R.id.toolbar) actually returns a toolbar (check your layouts that there is a Toolbar with that id)
or do not use getSupportActionBar without testing it for null.

Categories

Resources