Liron Navon Liron Navon - 6 months ago 17
Android Question

cant use ActionBar methods in android studio | Answered

I am trying to set some things to the action bar but i get the same errors on any method i use for it (e.g

actionBar.setTitle("my title");
or
actionBar.setBackgroundDrawable(R.drawable.actionbar_draw);
)

i cant even use the get methods for it...

they all give this error:

required android.support.v7.app.ActionBar but found android.app.ActionBar


i am using minimum sdk 15 and this is my code:

package com.tos.test.testactionbarapp;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {



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

ActionBar actionBar = getActionBar();
actionBar.setTitle("my title");


}
}


problem is, my app crashes whenever i start it because of this code and i get:

---


------ beginning of crash
05-28 15:32:00.403 11476-11476/ccom.tos.test.testactionbarapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tos.test.testactionbarapp, PID: 11476
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tos.test.testactionbarapp/com.tos.test.testactionbarapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.app.ActionBar.getTitle()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.app.ActionBar.getTitle()' on a null object reference
at com.tos.test.testactionbarapp.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
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:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Answer

you have to import android.support.v7.app.ActionBar and use getSupportActionBar() method od activity.