Danzeeeee
Danzeeeee

Reputation: 2630

ActionBar setBackgroundDrawable force close

Good day guys,My apps forced close and i get the log cat like this. I think the problem will be at Line 51 according to the LogCat but I have no idea why is it wrong.

The code at Line 51 will be :

bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00C4CD")));

MainActivity.java

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

    // c = MainActivity.this;
    ActionBar bar = getActionBar();

    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00C4CD")));

LogCat

11-07 18:25:40.526: E/AndroidRuntime(17342): FATAL EXCEPTION: main
11-07 18:25:40.526: E/AndroidRuntime(17342): Process: com.fyp.atms, PID: 17342
11-07 18:25:40.526: E/AndroidRuntime(17342): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp.atms/com.fyp.atms.MainActivity}: java.lang.NullPointerException
11-07 18:25:40.526: E/AndroidRuntime(17342):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at android.app.ActivityThread.access$800(ActivityThread.java:138)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at android.os.Looper.loop(Looper.java:136)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at android.app.ActivityThread.main(ActivityThread.java:5050)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at java.lang.reflect.Method.invokeNative(Native Method)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at java.lang.reflect.Method.invoke(Method.java:515)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at dalvik.system.NativeStart.main(Native Method)
11-07 18:25:40.526: E/AndroidRuntime(17342): Caused by: java.lang.NullPointerException
11-07 18:25:40.526: E/AndroidRuntime(17342):    at com.fyp.atms.MainActivity.onCreate(MainActivity.java:51)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at android.app.Activity.performCreate(Activity.java:5242)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-07 18:25:40.526: E/AndroidRuntime(17342):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)

Tried to add this in my values-> style.xml but still not working.

<resources>

    <style name="ActionBarTheme" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#00C4CD</item>
    </style> 



    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/ActionBarTheme</item>
    </style>

</resources>

LogCat after adding the styles.xml

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp.atms/com.fyp.atms.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

Upvotes: 1

Views: 758

Answers (3)

cmbind55
cmbind55

Reputation: 153

I had this issue and came up with the following implementation which worked in my case...

In AndroidManifest.xml, use the Theme.AppCompat style in the activity.

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat">
    ...

In the MainActivity.java code, make sure to 1) import the v7 ActionBarActivity, 2) have the activity extend ActionBarActivity, 3) use the corresponding v7 bar and getSupportActionBar() method.

import android.support.v7.app.ActionBarActivity;

...

public class MainActivity extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        android.support.v7.app.ActionBar bar = getSupportActionBar();
        if(bar != null){
            bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3f4deb")));
        }
    }

This is based on http://developer.android.com/guide/topics/ui/actionbar.html. I wasn't able to get the API 11 and higher methods to work, but the above solution was sufficient for my case.

Upvotes: 2

MrSilent
MrSilent

Reputation: 574

Try this:

bar.setBackgroundColor(Color.parseColor("#00C4CD"));

Upvotes: 0

buxik
buxik

Reputation: 2625

Try add

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

before ActionBar bar = getActionBar();

Upvotes: 0

Related Questions