I am trying to change the title of an Action Bar but I am having the null pointer exception, I am not very expert so I don't really know how to catch the exception or do it properly, basically this is the line I am having problem with:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setIcon(R.drawable.profile_banner_orange);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id){
case R.id.action_settings:
return true;
case R.id.profile_ic:
openItemManager();
return true;
}
return super.onOptionsItemSelected(item);
}
public void openItemManager(){
Intent myIntent = new Intent(MainActivity.this, ItemManagerActivity.class);
//myIntent.putExtra("key", value); //Optional parameters
MainActivity.this.startActivity(myIntent);
}
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blitzar.stiktag" >
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@drawable/icon_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activities.LoginActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize|stateVisible" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activities.ItemManagerActivity"
android:label="@string/title_activity_item_manager" >
</activity>
<activity
android:name=".Activities.MainActivity"
android:label="@string/title_activity_main"
android:icon="@drawable/profile_banner_orange" >
</activity>
</application>
</manifest>
and this is the activity_main XML:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.blitzar.stiktag.Activities.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="51dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tag"
android:id="@+id/button3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/search"
android:id="@+id/button4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/found"
android:id="@+id/button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/lost"
android:id="@+id/button2" />
</LinearLayout>
</RelativeLayout>
This is the log if that is any help:
08-12 21:37:39.113 450-450/com.blitzar.stiktag E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.blitzar.stiktag, PID: 450
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blitzar.stiktag/com.blitzar.stiktag.Activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setIcon(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setIcon(int)' on a null object reference
at com.blitzar.stiktag.Activities.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
Any help will be highly appreciated