android.support.v7.widget.Toolbar.getNavigationIcon()返回null

时间:2016-08-15 17:47:52

标签: android nullpointerexception drawable android-toolbar

我的项目中有一些课程......但有一个课程给我一些我无法单独解决的问题...

这是导致我麻烦的onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting);
    toolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.toolbar_002);
    toolbar.setBackgroundColor(Common.color);
    toolbar.setTitleTextColor(Common.compatible(Common.color, 0xFF000000) ? 0xFF000000 : 0xFFFFFFFF);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setTitle("Settings");
    colorFilter = new PorterDuffColorFilter(Common.compatible(Common.color, 0xFF000000) ? 0xFF000000 : 0xFFFFFFFF, PorterDuff.Mode.MULTIPLY);
    toolbar.getNavigationIcon().setColorFilter(colorFilter);
    toolbar.setNavigationOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            main = new Intent(SettingActivity.this, MainActivity.class);
            main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
            finish();
            startActivity(main);
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
    mFragmentTransaction.replace(R.id.container, new PrefsFragment());
    mFragmentTransaction.commit();
}

当我启动活动时,我会收到这些日志:

08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               FATAL EXCEPTION: main
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               Process: fr.zwedge.kingwarrior, PID: 27903
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.zwedge.kingwarrior/fr.zwedge.kingwarrior.SettingActivity}: java.lang.NullPointerException
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at android.app.ActivityThread.access$800(ActivityThread.java:151)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at android.os.Handler.dispatchMessage(Handler.java:110)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at android.os.Looper.loop(Looper.java:193)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at android.app.ActivityThread.main(ActivityThread.java:5299)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at java.lang.reflect.Method.invokeNative(Native Method)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Method.java:515)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at dalvik.system.NativeStart.main(Native Method)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               Caused by: java.lang.NullPointerException
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at fr.zwedge.kingwarrior.SettingActivity.onCreate(SettingActivity.java:113)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at android.app.Activity.performCreate(Activity.java:5264)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
08-15 18:48:11.709 27903 27903 E   AndroidRuntime                               ... 11 more

请...你能帮我解决这个问题吗? 谢谢,Darkball60。

1 个答案:

答案 0 :(得分:1)

我认为这部分getNavigationIcon()返回null。尝试首先设置导航图标,如下所示:

toolbar.setNavigationIcon(ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mtrl_am_alpha));

之后你不应该得到NullPointerException