-1

I've already looked at some questions over here regarding this issue but for so far, nothing seems to be doing it for me.

I'm trying something out and I wanted to change the color of the ActionBar in MainActivity instead of the XML file.

I'm still relatively new to Android Development so my apologies if this happens ends up being a minor error. My code is below (with a comment showing where the error is). Any idea why I'm getting a "NullPointerException"? Thank you very much:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (Build.VERSION.SDK_INT >= 11) {
        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff"))); // May produce "NullPointerException"
    }
}

EDIT: @Huy N. This is the screenshot. enter image description here

EDIT 2: Here's my "styles.xml" file.

<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>
</style>

And my "activity_main.xml"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.myname.checktime.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/testText"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

EDIT 3: This is my Android Manifest file, with the theme applied under "android:theme".

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myname.checktime">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

EDIT 4

Just ran it on my phone and it works on mine now. I'm on API 22 by the way. It works but will the "NullPointerException" possibly be a problem later? Is it safe if I ignore it? These are my logcats. I put them in pastebin because I couldn't get them to look good here. This is my logcat when I first connected my phone:

pastebin.com/908utuab

and this is it after I run the app.

pastebin.com/7Lf7Djev

For those who can't view Pastebin, this is the best I could do with getting my logcats here. I tried the code format but it would recognize part of my logcat and leave the rest as plain text.

This is by "Before" logcat (right after plugging in my phone).

06-25 02:42:22.640 18537-18537/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myname.checktime, PID: 18537 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.myname.checktime/com.myname.checktime.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.access$800(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:68) at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:146) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:190) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:172) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:512) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184) at com.myname.checktime.MainActivity.(MainActivity.java:17) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1606) at android.app.Instrumentation.newActivity(Instrumentation.java:1089) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  at android.app.ActivityThread.access$800(ActivityThread.java:155)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5343)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)  06-25 02:43:33.211 18832-18832/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myname.checktime, PID: 18832 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.myname.checktime/com.myname.checktime.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.access$800(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:68) at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:146) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:190) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:172) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:512) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184) at com.myname.checktime.MainActivity.(MainActivity.java:17) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1606) at android.app.Instrumentation.newActivity(Instrumentation.java:1089) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  at android.app.ActivityThread.access$800(ActivityThread.java:155)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5343)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)  06-25 03:39:12.540 27233-27233/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myname.checktime, PID: 27233 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myname.checktime/com.myname.checktime.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x3 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.access$800(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x3 at android.content.res.Resources.getText(Resources.java:299) at android.widget.Toast.makeText(Toast.java:285) at com.myname.checktime.MainActivity.onCreate(MainActivity.java:24) at android.app.Activity.performCreate(Activity.java:6010) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  at android.app.ActivityThread.access$800(ActivityThread.java:155)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5343)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)  06-25 03:51:57.799 2603-2603/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myname.checktime, PID: 2603 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myname.checktime/com.myname.checktime.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.access$800(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference at com.myname.checktime.MainActivity.onCreate(MainActivity.java:32) at android.app.Activity.performCreate(Activity.java:6010) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  at android.app.ActivityThread.access$800(ActivityThread.java:155)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5343)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)  06-25 04:24:41.901 25850-25850/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myname.checktime, PID: 25850 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myname.checktime/com.myname.checktime.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.access$800(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference at com.myname.checktime.MainActivity.onCreate(MainActivity.java:33) at android.app.Activity.performCreate(Activity.java:6010) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  at android.app.ActivityThread.access$800(ActivityThread.java:155)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5343)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)  06-25 12:53:52.375 15340-15340/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myname.checktime, PID: 15340 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myname.checktime/com.myname.checktime.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.access$800(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference at com.myname.checktime.MainActivity.onCreate(MainActivity.java:33) at android.app.Activity.performCreate(Activity.java:6010) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  at android.app.ActivityThread.access$800(ActivityThread.java:155)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5343)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

"After" Logcat

enter image description here

G_Man
  • 169
  • 1
  • 2
  • 12
  • Have you visit this article? http://stackoverflow.com/questions/29804825/change-action-bar-background-and-text-colours-programmatically-using-appcompat – xxx Jun 26 '16 at 02:30
  • 2
    is your app actually crashing with a NullPointerException, or are you just receiving the warning in the IDE? If its actually crashing, its useful to include a full stacktrace. If its just the warning message, the IDE is trying to tell you that `getActionBar` might return a null reference, and calling a method on that reference will produce the exception (in which case you might check if `bar != null` to stop that from happening). – trooper Jun 26 '16 at 03:07
  • 2
    Show us what Theme you are using. Also paste the layout of MainActivity – Farhad Jun 26 '16 at 03:12
  • @G_Man post your `styles.xml` file here ?? – Harshad Pansuriya Jun 26 '16 at 04:20
  • @FarhadFaghihi - Post has been edited. I have included my "styles.xml" and "activity_main.xml" files. – G_Man Jun 26 '16 at 04:25
  • have you applied theme to mainactivity in manifest? – user3215142 Jun 26 '16 at 04:41
  • @user3215142 - I just did but the issue still persists. I've included my modified manifest file. I changed `android:theme="@style/AppTheme"` to `android:theme="@style/Theme.AppCompat.Light.DarkActionBar"` – G_Man Jun 26 '16 at 05:02
  • In this case I believe it is safe to ignore the warning. Color.ParseColor(string) will return an int, or will raise an error if the string can't be parsed. Since your string is hard coded and valid, it should not ever raise the error. Java is complaining because it sees you passing in the new ColorDrawable object which may not be initialized. If you want to get rid of the warning, declare/initialize those arguments and check for null before calling setBackgroundDrawable(). Look at the post this was linked to as duplicate for further details. – Eric Jun 26 '16 at 18:33
  • @Eric: Okay. Thank you very much. – G_Man Jul 01 '16 at 00:21

2 Answers2

0

Have you tried this?

ActionBar actionBar = getSupportActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#95CDBA")));
        actionBar.setTitle(Html.fromHtml("<font color='#000099'>Hello World</font>"));
xxx
  • 3,315
  • 5
  • 21
  • 40
  • 1
    Yes. I tried it again a few moments ago too just to be sure. I'm still getting the same error unfortunately. I've added a screenshot this time. – G_Man Jun 26 '16 at 02:59
  • 1
    Try to add this line before setContentView(): getWindow().requestFeature(Window.FEATURE_ACTION_BAR) show me what theme you are using? – xxx Jun 26 '16 at 03:39
  • Same issue unfortunately. I have edited my post with my "styles.xml" and "activity_main.xml" files by the way. – G_Man Jun 26 '16 at 04:29
  • show your logcat and android version you're running – xxx Jun 26 '16 at 04:49
  • Just a moment. I'll add them. – G_Man Jun 26 '16 at 04:54
  • omg, I can not find out any problem on your project, I just tried it on my phone and it actually work well, how about your logcat? – xxx Jun 26 '16 at 05:04
  • Just ran it on my phone. It works on mine now. I'm on API 21. But if you're still interested, this is my logcat when I plugged my phone in. I uploaded it in pastebin because I couldn't get it to look good when I edited the question: http://pastebin.com/908utuab And this is it after I run the app. The "may produce a NullPointerException" error still exists though but I don't see anything about it in the "after" logcat: http://pastebin.com/7Lf7Djev – G_Man Jun 26 '16 at 05:17
  • I couldn't open your link :) – xxx Jun 26 '16 at 05:23
  • Hmm, alright. I'll try to get the logcats here and make it look as neat as possible. Just a moment. – G_Man Jun 26 '16 at 05:27
  • I've added the logcats to the best of my ability. My apologies if they don't look too good. I tried to use the `code` format but it would recognize part of my logcats and ignore the rest. – G_Man Jun 26 '16 at 05:46
0

All I see from what you posted is a warning in your IDE. I don't want to discourage you from fixing it, because you probably should, but is the app actually crashing with this error or are you just getting the warning?

Eric
  • 673
  • 5
  • 18