麻烦实施材料主题 [英] Trouble implementing Material Theme
问题描述
我使用rel="nofollow">这 空指针异常每一次我称之为 getActionBar()。事情
或 getSupportActionBar()。事情
。
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.
在值/ styles.xml
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>
在我的价值观-V21 / styles.xml
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>
正如我已经设置了windowActionBar为假的,我有一个工具栏的布局。
As I have set the windowActionBar to false, I have a toolbar layout.
toolbar.xml
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"/>
最后到MainActivity.java
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"
我的logcat的:
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
我的问题:
- 为什么我得到一个空指针异常的getActionBar(),当我加入我的工具栏布局的MainActivity?
- 如何添加材料的设计主题,以我的应用程序。
干杯, Rakshak
Cheers, Rakshak
推荐答案
更新:您的实际问题是,你有&LT; android.support.v7.widget。工具栏
code在自己的 toolbar.xml
文件。这必须在 activity_main.xml
,并要与一个动作条使用所有其他的布局。
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.
工具栏比默认动作条(主要是外观相关的)更多的定制。如果你想要或需要自定义动作条,你需要设置一个工具栏的动作条。如果不。你不需要做到这一点,你可以简单地使用由主题(你必须删除windowActionBar虚假部分来自你的主题)。
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"/>
那么你可以使用你的code。
then you can use your code.
但你也应该确保 getSupportActionBar()
不使用它之前返回null。在情况下,布局缺少的工具栏。
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回答以下
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.
也是你对他们你有&LT;项目名称=windowActionBar&GT;假&LT; /项目&GT;
禁用默认的动作条
also in your them you have <item name="windowActionBar">false</item>
that disables the default actionbar.
您可以执行以下操作:
- 设置
windowActionbar
来在你的主题为真,那么你的默认动作条,你不会需要的工具栏。 - 如果您想使用工具栏上的非默认位置,你必须确保
findViewById(R.id.toolbar)
实际上返回一个工具栏(检查你的布局,有一个工具栏与ID) - 在或者不使用getSupportActionBar没有测试它为空。
- 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.
这篇关于麻烦实施材料主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!