2

I have an application that creates an Intent when selecting item menu, but the first time the user clicks this button, the app crashes with the following stack trace.

E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.systemsat.monitriip, PID: 17818
java.lang.NoClassDefFoundError: br.com.systemsat.monitriip.activity.ConfigurationActivity
    at br.com.systemsat.monitriip.activity.MainActivity.onOptionsItemSelected(MainActivity.java:212)
    at android.app.Activity.onMenuItemSelected(Activity.java:2912)
    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:421)
    at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:188)
    at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
    at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:663)
    at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
    at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
    at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
    at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:947)
    at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
    at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:155)
    at android.view.View.performClick(View.java:5201)
    at android.view.View$PerformClick.run(View.java:21163)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Below is my Activity declaration inside AndroidManifest.xml:

<activity
        android:name=".activity.ConfigurationActivity"
        android:parentActivityName=".activity.MainActivity">
    </activity>

Also, below is the line that is accused to be the cause of the problem by the call stack:

Intent hashCodeIntent = new Intent(getApplicationContext(), ConfigurationActivity.class);

Some answers found in StackOverflow pointed me that clean and rebuild would do the trick but I've tried without any luck. I have also tried adding MultiDex to gradle file as well as to my custom Application class, but that also resulted in a no go.

Could someone else have any insights as to what I might have done wrong? What is really intriguing is that this error only happens on the first time the user clicks the "Configuration" screen button. After the app crashes, the user can reopen it and click the button normally.

Thank you in advance.

UPDATE

I have forgot to mention that Android Monitor also logs the following info right before the crash:

Rejecting re-init on previously-failed class java.lang.Class<br.com.systemsat.monitriip.activity.ConfigurationActivity>

UPDATE 2

Here is my onOptionsItemSelected method:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_action_config:
            Intent hashCodeIntent = new Intent(this, ConfigurationActivity.class);
            hashCodeIntent.putExtra("isConfigurationCall", true);
            startActivity(hashCodeIntent);
            break;
    }
    return true;
}

UPDATE 3

I have just generated an apk for testing purposes and the error did not show. Maybe it has something to do with debuging? Or even something to do with gradle version? I currently use the last version of gradle.

  • 1
    change `getApplicationContext()` to `this` – petey Aug 11 '16 at 19:54
  • If the sollution from petey will not work when post a full code, pls. – Denis Sologub Aug 11 '16 at 19:56
  • Have you tried clean building the project? –  Aug 11 '16 at 19:57
  • You should post all your code not just the part **you** are suspicious about. – Masked Man Aug 11 '16 at 20:01
  • @petey changing getApplicationContext() to this did not work. tynn: Proguard is not enabled. My project has a proguard-rules.pro file but it is all commented. ArkayCZ: I have. I also tried cleaning from Mac terminal. No luck there also. Шах and MaskedMan: What parts of code would you want to see? It is not possible to share the whole solution since this code is my companie's and not mine. Please, look at edit in Question. I forgot to mention something that I think is relevant. – Pedro Yusim Aug 11 '16 at 20:25
  • Please, post your code of the MainActivity.onOptionsItemSelected method – Alexey Rogovoy Aug 11 '16 at 20:48
  • @AlexeyRogovoy I have updated my answer with the method. – Pedro Yusim Aug 11 '16 at 20:55
  • Have you tried removing the extra context from the R.id.menu_action_config case and just consoling the output whether the menu is working? This way you can narrow down the error. – Smit Aug 12 '16 at 04:30
  • @Smit I have tried commenting the extra but the error still pops up. I have also tried changing `Intent hashCodeIntent = new Intent(this, ConfigurationActivity.class);` to `Intent hashCodeIntent = new Intent(this, HashCodeActivity.class);` where HashCodeActivity is some other Activity in my project. This test did not result in a crash the first time I clicked the button. Which has me thinking the error is somehow connected to ConfigurationActivity. Stiil no clues to where though. – Pedro Yusim Aug 12 '16 at 13:03
  • @PedroYusim Brillent. Let me take a look – Smit Aug 12 '16 at 16:47
  • @PedroYusim Can you provide your ConfigurationActivity.class ? – Smit Aug 12 '16 at 16:54
  • @Smit I have put my ConfigurationActivity in CodeShare. Just follow this: https://codeshare.io/3iYus . Also, have you read the APK thing I wrote in my answer? The error does not happen when running from APK. Really strange. – Pedro Yusim Aug 15 '16 at 13:13
  • @PedroYusim why are you doing like `.activity.cong...` and `.activity.ConfigurationActivity` ?why not `.MainActivity` and `.ConfigurationActivity` – Smit Aug 15 '16 at 13:16
  • @Smit It is just for organization purposes. I have created an activity package under my root folder. That is why I use .activity. – Pedro Yusim Aug 15 '16 at 13:30
  • Oh ok. Then can u try the snippet i have sent. Just replace the activity name by the activity name that u preffered! – Smit Aug 15 '16 at 14:20
  • Sounds to me like instant run / debug glitch, specially after you mentioned generating apk. How exactly are you running the crashed app? Is it with a clean install? What AS version? Can you reproduce without opening AS? – Fabio Aug 23 '16 at 14:15
  • @Fabio I am currently using AS 2.1.2. The error no longer occurs and I haven't done anything towards fixing it. I have disabled the AS Instant Run mode since I am working with SugarORM and it is not recommended. Thank you all for your help! – Pedro Yusim Aug 23 '16 at 18:52

1 Answers1

0

Try to change getAplicationContext() for getActivity()

Sometimes getAplicationContext returns null.

  • I cannot call getActivity() in that context. But changing to this as @petey advised didn't work also. – Pedro Yusim Aug 11 '16 at 20:52
  • I would say it is the other way around. In case you have ever wondered what is the difference between contexts check these links https://possiblemobile.com/2013/06/context/ AND http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context AND http://stackoverflow.com/questions/987072/using-application-context-everywhere/987503#987503 – Todor Kostov Aug 11 '16 at 21:26