1

I have some classes in my project...but one gives me some problems that I can't solve alone...

Here is the onCreate method that causes me trouble :

@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();
}

And when I launch the activity, I get these logs :

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

Please...can you help me fix this problem ? Thanks, Darkball60.

Mesabloo
  • 337
  • 4
  • 13

1 Answers1

1

I think this part getNavigationIcon() returns null. Try to set the navigation icon first like this:

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

After that you should not be getting a NullPointerException.

Vucko
  • 7,371
  • 2
  • 27
  • 45
  • I added the line you suggested...and...it works...but now...my color filter doesn't work and the arrow keep being black instead of white...why ? – Mesabloo Aug 15 '16 at 18:17
  • I think that it takes care of the color itself choosing the black one on a lighter backgrounds and the white one on the darker backgrounds. I use this same code and my arrow is white. You can google a bit about it :) – Vucko Aug 15 '16 at 18:18
  • Ow...how could it be possible ? – Mesabloo Aug 15 '16 at 18:20
  • Don't know honestly :D Analyzing the toolbar color and then using a different colored arrow I presume. Should not be extra hard to implement. If you don't like that, just use a regular white arrow and it'll work. – Vucko Aug 15 '16 at 18:22
  • But I can't...cause I have to verify if background color is dark or not...and then...change arrow color to white if background color is dark...and black if it's light. – Mesabloo Aug 15 '16 at 18:25
  • And...in another part of this class...when I use a specific method...it works...and I tested to call this method at this point...but it doesn't work... – Mesabloo Aug 15 '16 at 18:30
  • I did not say that you need to take care of the background. I said that I think that it's done automatically if you use `ContextCompat` and refer to [this link](http://stackoverflow.com/questions/26788464/how-to-change-color-of-the-back-arrow-in-the-new-material-theme) – Vucko Aug 15 '16 at 18:32
  • And does it help ? What is the actual problem you're facing? You want the arrow to **always** be one color or not? – Vucko Aug 15 '16 at 18:53
  • 1
    OK...so...I just read this post another time...and found the solution that works : I just have to use getSupportActionBar().setHomeAsUpIndicator(upArrow) and it works great – Mesabloo Aug 15 '16 at 18:58
  • I just want that the arrow changes color from white to black (or black to white) if the background is dark or light...just to make the arrow always visible when user changes toolbar background color – Mesabloo Aug 15 '16 at 18:59
  • Just one question...how do I mark this question as solved ? – Mesabloo Aug 15 '16 at 19:04
  • Thanks @Vucko for this...It just solved my problem in time...I thought I won't find the solution alone. – Mesabloo Aug 15 '16 at 19:48