0

Iam new to android/coding/Java, please go easy on me if mistakes

Iam trying to reproduce same Action bar for the sub activities of my Main Activity (LoginActivity) here is my LoginActivity

public class LoginActivity extends AppCompatActivity {

    ActionBar actionBar;

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

        actionBar = getActionBar();// ActionBar Customisations
                actionBar.setTitle("Klok Innovations");
                actionBar.setLogo(R.drawable.collection_report);
                actionBar.setDisplayUseLogoEnabled(true);
                actionBar.setDisplayShowHomeEnabled(true);

The app crashes when i open it ... Here is the logcat

07-04 17:24:15.005 7636-7636/online.klok.mobpos E/AndroidRuntime: FATAL EXCEPTION: main
  Process: online.klok.mobpos, PID: 7636
  Theme: themes:{}
  java.lang.RuntimeException: Unable to start activity ComponentInfo{online.klok.mobpos/online.klok.mobpos.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
      at android.app.ActivityThread.-wrap11(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5461)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
      at online.klok.mobpos.LoginActivity.onCreate(LoginActivity.java:28)
      at android.app.Activity.performCreate(Activity.java:6251)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5461) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Also how can I create bar at the bottom of the screen and appear in all my sub activities?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Sooraj S
  • 291
  • 5
  • 13

2 Answers2

0

Use

getSupportActionBar();

instead of

getActionBar();
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0

this is why because your actionbar getting null pointer exeption:

try like this

assert actionbar != null;
if(actionbar != null){
   **write your code here**
}

now its never be null..

  1. you are using appCompateActivity so use getSupportActionBar. add this lines in your every subActivity in onCreate method.

         assert getSupportActionBar() != null;
       // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setTitle("Your Title Here");
    

that commented line is for back button at upper left corner.. if you want it then uncomment it. and write some line in manifest to do some back button action.

<activity android:name=".yourSubActivityName">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".whichActivityYouWantToGo" />
</activity>
  1. if you are in mainActivity and want to exit app using dialog.. here is the code.

    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("Are you sure you want to exit?")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                                finish();
                            }
                        })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
        // Create the AlertDialog object and return it
        builder.create();
        builder.show();
    }
    
Sagar Chavada
  • 5,169
  • 7
  • 40
  • 67
  • The thing is i want to populate it to all my Sub activities ... is it possible without creating it on every activity – Sooraj S Jul 05 '16 at 05:19
  • what do you mean by populating.? – Sagar Chavada Jul 05 '16 at 05:26
  • I want to display the action bar on my sub activities ... without setting views on each of them – Sooraj S Jul 05 '16 at 09:47
  • ok.. first tell me what is your latest api that you testing your app.. otherwise show me your gradle so i can understand everything, after that i will modify my answer – Sagar Chavada Jul 05 '16 at 09:55
  • My build.gradle [link](http://hastebin.com/yubeninora.vbs) what if i want to exit App if pressed back button ie when iam on parent activity with an Alert box for exit ing – Sooraj S Jul 05 '16 at 10:29
  • first of all change your dependency version 23.4.0 to 23.3.0 because your using buildToolVersion is 23.0.3 so. – Sagar Chavada Jul 05 '16 at 10:57
  • i hope my updated answer solve all your question.. – Sagar Chavada Jul 05 '16 at 11:08
  • thanks @Sagar Chavada – Sooraj S Jul 05 '16 at 11:17
  • still have a few more questions ... would you mind if we take this to a better chat client something like hangouts or ....? If you agree can i have your mail id ? ............... PS : less than 15 rep , they wouldn't allow me to up-vote it – Sooraj S Jul 05 '16 at 11:20