My target is to apply style on individual Views. I've created a style in styles.xml like this:
<style name="trLinearLayout">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">vertical</item>
<item name="android:background">#android:color/white</item>
</style>
the corresponding ID is generated in R.java and the following code is using the above style:
XmlResourceParser parser = context.getResources().getXml(R.style.apptheme_black);
AttributeSet attribs = Xml.asAttributeSet(parser);
LinearLayout tr = new LinearLayout(context, attribs);
I get following exception while running:
04-10 16:41:52.053: W/ResourceType(283): Requesting resource 0x7f050005 failed because it is complex
04-10 16:41:52.065: W/System.err(283): android.content.res.Resources$NotFoundException: Resource ID #0x7f050005
04-10 16:41:52.065: W/System.err(283): at android.content.res.Resources.getValue(Resources.java:891)
04-10 16:41:52.065: W/System.err(283): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1865)
04-10 16:41:52.065: W/System.err(283): at android.content.res.Resources.getXml(Resources.java:779)
What am I doing wrong?
getXml method is used for fetching xml resources out from res/xml folder.
Related
I am having a problem implementing Material Button and I get the following error shown below. I have tried different solutions found on stackoverflow but can't seem to know why it keeps showing me this error. Any help would be much appreciated. Thanks in advance
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.lifeadvicequotes, PID: 8603
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.lifeadvicequotes/com.app.lifeadvicequotes.MainActivity}: android.view.InflateException: Binary XML file line #73 in com.app.lifeadvicequotes:layout/activity_main: Binary XML file line #73 in com.app.lifeadvicequotes:layout/activity_main: Error inflating class com.google.android.material.button.MaterialButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4111)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4277)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8751)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
Caused by: android.view.InflateException: Binary XML file line #73 in com.app.lifeadvicequotes:layout/activity_main: Binary XML file line #73 in com.app.lifeadvicequotes:layout/activity_main: Error inflating class com.google.android.material.button.MaterialButton
Caused by: android.view.InflateException: Binary XML file line #73 in com.app.lifeadvicequotes:layout/activity_main: Error inflating class com.google.android.material.button.MaterialButton
This is my style xml
<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="android:textColorPrimary">#color/text_color_primary</item>
</style>
<!-- Base application theme. -->
<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.Button" parent="android:Widget.Material.Button">
<item name="colorButtonNormal">#FFFFFF</item>
<item name="android:textColor">#000000</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="NavigationDrawerStyle">
<item name="android:textSize">16sp</item>
</style>
</resources>
The issue might be from parent="android:Widget.Material.Button" try changing it to parent="Widget.MaterialComponents.Button or parent="Base.Widget.AppCompat.Button" if you haven't imported Material Components library
I am attempting to upgrade my react-native version to 0.59.10. My app crashes upon attempt to debug and I get this error when running adb logcat.
E/AndroidRuntime( 9402): Process: com.attendance, PID: 9402
E/AndroidRuntime( 9402): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.attendance/com.attendance.MainActivity}: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }```
This is my current style code:
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:navigationBarColor">#color/primary_dark</item>
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<item name="android:colorAccent">#color/accent</item>
<item name="android:windowBackground">#color/primary_dark</item>
</style>
<application
android:name=".MainApplication"
android:allowBackup="false"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:theme="#style/AppTheme">
I can get the app to run by simply changing android:theme to
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
but the theme is not the same as before.
I have tried implementing all the properties in the initial error but then the app never loads.
Any ideas on how to get my app to load and have the original themes?
Modified styles to:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorPrimary">#color/primary</item>
<item name="colorAccent">#color/accent</item>
<item name="android:navigationBarColor">#color/primary_dark</item>
</style>
</resources>```
and this resolved the issue.
Here's my IDE and gradle plugin version:
AndroidStudio:3.1.4
classpath 'com.android.tools.build:gradle:3.1.4'
In my project,I have a main module and a sub module.
In my sub module, I have a values.xml like following:
<style name="OmActivityAnim">
<item name="android:activityOpenEnterAnimation">#anim/om_base_slide_from_right</item>
<item name="android:activityOpenExitAnimation">#anim/om_base_slide_to_left</item>
<item name="android:activityCloseEnterAnimation">#anim/om_base_slide_from_left</item>
<item name="android:activityCloseExitAnimation">#anim/om_base_slide_to_right</item>
</style>
<style name="OmActivityAnim2">
<item name="android:activityOpenEnterAnimation">#anim/om_base_show_dialog_anim</item>
<item name="android:activityOpenExitAnimation">#anim/om_base_dismiss_dialog_anim</item>
<item name="android:activityCloseEnterAnimation">#anim/om_base_show_dialog_anim</item>
<item name="android:activityCloseExitAnimation">#anim/om_base_dismiss_dialog_anim</item>
</style>
but when I run my Main module I just got
AAPT: error: style attribute '#android:attr/activityOpenEnterAnimation' not found.
following errors
error: style attribute '#android:attr/activityOpenEnterAnimation' not found.
error: style attribute '#android:attr/activityOpenExitAnimation' not found.
error: style attribute '#android:attr/activityCloseEnterAnimation' not found.
error: style attribute '#android:attr/activityCloseExitAnimation' not found.
error: style attribute '#android:attr/activityOpenEnterAnimation' not found.
error: style attribute '#android:attr/activityOpenExitAnimation' not found.
error: style attribute '#android:attr/activityCloseEnterAnimation' not found.
error: style attribute '#android:attr/activityCloseExitAnimation' not found.
But, why my android:activityOpenEnterAnimation changes to '#android:attr/activityOpenEnterAnimation'?
Just add '#' before 'android'.Because '#android' is the exact annotation to write the theme reference to another resource.
Edit your code from
<style name="OmActivityAnim">
<item name="android:activityOpenEnterAnimation">#anim/om_base_slide_from_right</item>
<item name="android:activityOpenExitAnimation">#anim/om_base_slide_to_left</item>
<item name="android:activityCloseEnterAnimation">#anim/om_base_slide_from_left</item>
<item name="android:activityCloseExitAnimation">#anim/om_base_slide_to_right</item>
</style>
<style name="OmActivityAnim2">
<item name="android:activityOpenEnterAnimation">#anim/om_base_show_dialog_anim</item>
<item name="android:activityOpenExitAnimation">#anim/om_base_dismiss_dialog_anim</item>
<item name="android:activityCloseEnterAnimation">#anim/om_base_show_dialog_anim</item>
<item name="android:activityCloseExitAnimation">#anim/om_base_dismiss_dialog_anim</item>
</style>
to
<style name="OmActivityAnim">
<item name="#android:activityOpenEnterAnimation">#anim/om_base_slide_from_right</item>
<item name="#android:activityOpenExitAnimation">#anim/om_base_slide_to_left</item>
<item name="#android:activityCloseEnterAnimation">#anim/om_base_slide_from_left</item>
<item name="#android:activityCloseExitAnimation">#anim/om_base_slide_to_right</item>
</style>
<style name="OmActivityAnim2">
<item name="#android:activityOpenEnterAnimation">#anim/om_base_show_dialog_anim</item>
<item name="#android:activityOpenExitAnimation">#anim/om_base_dismiss_dialog_anim</item>
<item name="#android:activityCloseEnterAnimation">#anim/om_base_show_dialog_anim</item>
<item name="#android:activityCloseExitAnimation">#anim/om_base_dismiss_dialog_anim</item>
</style>
This will solve your issue.
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.
I applied the below style for timepicker:
<style name="MyHoloTimePicker" parent="#android:style/Widget.DeviceDefault.DatePicker">
<item name="android:textColor">#2eb135</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">normal</item>
</style>
I have set the above style to time picker in xml as follows:
<TimePicker
android:id="#+id/pickertime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/MyHoloTimePicker"
/>
I got the below exception:
12-03 01:12:26.742: E/AndroidRuntime(9279): Caused by: java.lang.NullPointerException <br>
12-03 01:12:26.742: E/AndroidRuntime(9279): Caused by: java.lang.reflect.InvocationTargetException
How can I sort out this issue?
There is no Widget.DeviceDefault.DatePicker. Try setting the parent to:
parent="#android:style/Widget.DatePicker"
EDIT:
I found that the DeviceDefault themes are in a different file and can by used by API 14+.
themes_device_defaults.xml