我要在导航抽屉中添加阴影。这是我使用的代码。
创建了一个navbar_shadow.xml
并将其添加到drawable中。这是代码。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#111"
android:endColor="#00000000">
</gradient>
<size
android:height="@dimen/activity_vertical_margin"
android:width="5dp">
</size>
然后将以下代码添加到NavigationDrawer.java
mDrawerLayout.setDrawerShadow(R.drawable.navbar_shadow, GravityCompat.START);
所以,当我运行此代码时。它开始崩溃并跟随错误显示。这是我得到的Logcat。
11-27 09:43:47.787 6541-6541/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myayubo, PID: 6541
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myayubo/com.myayubo.Extract}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.myayubo.Extract.onCreate(Extract.java:63)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
请帮我成功添加我的影子。感谢。
答案 0 :(得分:1)
确保您使用mDrawerLayout
findViewById
编辑1: 更改订单
首先使用findViewById初始化布局,然后将drawable添加到布局
答案 1 :(得分:1)
你应该使用findViewById
初始化mDrawerLayout首先findViewById然后将drawable添加到布局
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_extract);
mDrawerLayout.setDrawerShadow(R.drawable.navbar_shadow, GravityCompat.START);
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.navbar_shadow, Gravity.LEFT);/// it should be here after findviewbyid
mLenear = (ScrollView)findViewById(R.id.leftDrawer);
答案 2 :(得分:0)
您对setDrawerShadow的调用需要在findViewById
之后即
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.navbar_shadow, GravityCompat.START);
目前,您正在尝试在找到视图之前应用阴影,因此是空指针。