Search code examples
javaandroidandroid-activity

bottom bar button crash


I have been trying to figure out why suddenly my bottom bar navigation button for a particular activity causes crashes.

On MainActivity, I have the following bottom bar nav:

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                //mTextMessage.setText(R.string.title_home);
                return true;
            case R.id.navigation_events:
                //mTextMessage.setText(R.string.title_events);
                return true;
            case R.id.navigation_offpeak:
                //mTextMessage.setText(R.string.title_offpeak);
                return true;
            case R.id.navigation_deals:
                Intent reservationsIntent = new Intent(MainActivity.this, DealsHomeActivity.class);
                startActivity(reservationsIntent);
                //mTextMessage.setText(R.string.title_bookings);
                return true;
            case R.id.navigation_me:
                Intent memberProfileIntent = new Intent(MainActivity.this, MemberProfileActivity.class);
                startActivity(memberProfileIntent);
                //logout();
                //mTextMessage.setText(R.string.title_me);
                return true;
        }
        return false;
    }
};

Then, my activity is instantiated like all my other activities, like the MemberProfileActivity.class.

However pushing the button to get to MemberProfileActivity.class works as expected on the phone, but when doing this for DealsHomeActivity.class results in a crash with the following logcat:

05-27 15:38:40.473 31978-31978/asia.diningcity.android E/AndroidRuntime: FATAL EXCEPTION: main Process: asia.diningcity.android, PID: 31978 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{asia.diningcity.android/asia.diningcity.android.activities.DealsHomeActivity}: java.lang.InstantiationException: java.lang.Class cannot be instantiated at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2911) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1608) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6665) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) Caused by: java.lang.InstantiationException: java.lang.Class cannot be instantiated at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1174) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2727) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2911) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1608) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6665) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)

I have been looking at the two activities in Java to see if there are any differences in the onCreate method to start the activity but none.

So I don't understand where this could be coming from.

Please note also this was working fine before, and since then I haven't touched the menu bar or that Deals activity so I don't know what could have caused this.


Solution

  • i believe your class is either an abstract class or a private class, abstract or private classes can not be instantiated,

    Abstract classes are by definition not instantiable.

    if your class is abstract or private, i assume it's something like this

    private abstract class DealsHomeActivity extends AppCompatActivity
    

    change to

    public class DealsHomeActivity extends AppCompatActivity