-2

I tried to Add Up Button into Action Bar in RegisterActivity but it takes me out of the app without crash ,and warning tells that setDisplayHomeAsUpEnabled may produce NullPonterException , I follow instruction in Android Developer console and see also a lot of questions similar to my case but cold not implement the answers to my case ,so I need some one to help me in my case . and here is my code :

RegisterActivity :

public class RegisterActivity extends AppCompatActivity {
private Toolbar mToolbar;

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

    mToolbar = (Toolbar) findViewById(R.id.register_toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("Creat Account");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

 }

I try this also but didnt work :

if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle("Creat Account");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

activity_register.xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mohamed71.lapitchat.RegisterActivity">

<include
    android:id="@+id/register_toolbar"
    layout="@layout/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="0dp" />

app_bar_layout.xml :

  <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar

xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
android:minHeight="?attr/actionBarSize"
>

</android.support.v7.widget.Toolbar>

AndriodMainifest.xml :

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

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

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

enter image description here

tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
Mohamed Ahmed
  • 57
  • 1
  • 10
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Aug 16 '17 at 09:09

2 Answers2

1

You are adding wrong toolbar id . try to change your code

mToolbar = (Toolbar) findViewById(R.id.main_app_bar);
    setSupportActionBar(mToolbar);

instead of

mToolbar = (Toolbar) findViewById(R.id.register_toolbar);
    setSupportActionBar(mToolbar);
Vinayak B
  • 4,430
  • 4
  • 28
  • 58
0

You can try as following:

mToolbar = (Toolbar) findViewById(R.id.register_toolbar);
setSupportActionBar(mToolbar);

android.support.v7.app.ActionBar ab = getSupportActionBar();
if(ab != null){
    ab.setTitle("Creat Account");
    ab.setDisplayHomeAsUpEnabled(true); 
    ab.setDisplayShowHomeEnabled(true);
}  
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34