-1

My android application crashes in emulator. The application did not open in emulator. I am working with java. It was working perfectly before i added the bottom navigation into my application. Please sort out my issue as i am new in this field. Here is the code of my main activity below.

package com.example.bottomnavigation;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import com.example.bottomnavigation.databinding.ActivityMainBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
ImageView picture, video, message, mail;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    getSupportActionBar().hide();
    binding.bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.first:
                    FragmentTransaction firsttrans = getSupportFragmentManager().beginTransaction();
                    firsttrans.replace(R.id.frame, new HomeFragment());
                    firsttrans.commit();
                    break;
                case R.id.search:
                    FragmentTransaction searchtrans = getSupportFragmentManager().beginTransaction();
                    searchtrans.replace(R.id.frame, new SearchFragment());
                    searchtrans.commit();
                    break;
                case R.id.exit:
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("Exit")
                            .setIcon(R.drawable.exit)
                            .setMessage("Are you sure you want to exit")
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finishAffinity();
                                }
                            }).show();

            }
            return true;
        }
    });
    picture = findViewById(R.id.arham);
    picture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Pictures.class);
            startActivity(intent);
        }
    });
}

}

Here is the logcat

2021-04-05 12:35:38.662 19375-19375/? E/ottomnavigatio: Unknown bits set in runtime_flags: 0x8000
2021-04-05 12:35:40.544 19375-19375/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bottomnavigation, PID: 19375
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bottomnavigation/com.example.bottomnavigation.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.hide()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.hide()' on a null object reference
        at com.example.bottomnavigation.MainActivity.onCreate(MainActivity.java:27)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • add logs from logcat too. check logcat it will show you where exactly your code breaks, – kelvin Apr 05 '21 at 07:12
  • 2021-04-05 11:37:30.036 2115-2149/? E/AppIdleHistory: Error writing app idle file for user 0 2021-04-05 11:57:53.351 2115-2149/? E/AppIdleHistory: Error writing app idle file for user 0 – Abdul Basit Apr 05 '21 at 07:15
  • check for long block of red lines, in which you will find a class name in blue color – kelvin Apr 05 '21 at 07:23
  • now there is another error 2021-04-05 12:29:19.869 1774-2466/? E/GnssHAL_GnssInterface: gnssSvStatusCb: b: input svInfo.flags is 8 – Abdul Basit Apr 05 '21 at 07:30
  • keep your logcat open try to reproduce that error at that very moment you will see a long block of red lines. – kelvin Apr 05 '21 at 07:32
  • These aren't errors from your app. They may not be errors at all, they're log messages with ERROR level of importance. It's safe to ignore these. – Eugen Pechanec Apr 05 '21 at 07:39
  • Crashes from your app will look like [this](https://developer.android.com/studio/debug/stacktraces). That's the useful part. I don't see it in your question. Without it we can't help. – Eugen Pechanec Apr 05 '21 at 07:40
  • sorry for that i have updated the errors now please help me out – Abdul Basit Apr 05 '21 at 07:47

1 Answers1

2

The exception is being thrown because you are calling hide() on a null reference to an ActionBar. Assuming that this is the relevant code, it is occurring on this line:

    getSupportActionBar().hide();

If you read the javadocs (here and here) for getSupportActionBar() you will see that it returns null if the activity doesn't have an action bar. That must be happening here.

(So why is your app trying to hide the activity bar when it doesn't have one??)

It would appear that the solution is to delete the offending line. Or if there are contexts in which your app might have an action bar, change it to:

    ActionBar bar = getSupportActionBar();
    if (bar != null) {
        bar.hide();
    }
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216