currently iam working on Android Application.in that application i only face one issue.the problem is in the first line of this code.when iam trying to lunch the application but it won’t work
this is the code part of it:
getSupportActionBar().setTitle("All My Notes");
ActionBar actionBar=getSupportActionBar();
if(actionBar!=null){
actionBar.setTitle("My Notes");
}
mcreatenotesfab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(notesactivity.this, createnote.class));
}
});
This is the Logcat of the error:
2024-07-03 13:52:33.787 6285-6285 AndroidRuntime com.example.mynotesapp E FATAL EXCEPTION: main
Process: com.example.mynotesapp, PID: 6285
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mynotesapp/com.example.mynotesapp.notesactivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)’ on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)’ on a null object reference
at com.example.mynotesapp.notesactivity.onCreate(notesactivity.java:69)
at android.app.Activity.performCreate(Activity.java:8595)
at android.app.Activity.performCreate(Activity.java:8573)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3764)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
—————————- PROCESS ENDED (6285) for package com.example.mynotesapp —————————-
i tried commenting the first line of the provided code.And after lunched the application it lunched sucessfully but not showing the Action Bar of my application.Iam expecting to implement the Action Bar of my application
//getSupportActionBar().setTitle("All My Notes");
ActionBar actionBar=getSupportActionBar();
if(actionBar!=null){
actionBar.setTitle("My Notes");
}
mcreatenotesfab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(notesactivity.this, createnote.class));
}
});
Dineth Jayawickrama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.