gak
gak

Reputation: 32763

java.lang.NoSuchMethodError: android.app.AlertDialog$Builder

I'm getting reports about this particular exception and it has stumped me, without any luck searching around for it. This app has been tested on a handful of different devices without anything resembling it.

The strange thing is that the first time the user runs the app, an AlertDialog.Builder is created and presented, so AlertDialog.Building is most likely a real method on the device. The docs also tell me that it is also been around since API level 1.

Could the exception be referring to some other problem?

The exceptions happened on a GT-N7000 Galaxy Note running 2.3.6.

The stack trace is deobfuscated:

java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.
at com.slowchop.wifiheat.lib.heat.Filter.void showSubjectDialog(java.lang.String,java.lang.String)(SourceFile:143)
at com.slowchop.wifiheat.lib.heat.Filter.void showChangeAccessPoint()(SourceFile:128)
at com.slowchop.wifiheat.lib.heat.HeatActivity.boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem)(SourceFile:475)
at android.support.v4.app._ActionBarSherlockTrojanHorse.boolean onMenuItemSelected(int,com.actionbarsherlock.view.MenuItem)(SourceFile:119)

The method involved:

private void showSubjectDialog(String title, final String filter) {
    new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_DARK)
            .setTitle(title)
            .setAdapter(subjectAdapter, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int i) {
                    ListItem item = (ListItem)subjectAdapter.getItem(i);
                    updateFilterFromAlertDialog(dialog, filter, item.id);
                }
            }).show();
}

Line 143 is:

    new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_DARK)

There was also another stack trace that comes from a different path, but continues with calling showChangeAccessPoint() in the same manner. In this case, the error about AlertDialog.Builder is called from another AlertDialog.Builder!:

java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.
at com.slowchop.wifiheat.lib.heat.Filter.void showSubjectDialog(java.lang.String,java.lang.String)(SourceFile:143)
at com.slowchop.wifiheat.lib.heat.Filter.void showChangeAccessPoint()(SourceFile:128)
at com.slowchop.wifiheat.lib.heat.Filter$2.void onClick(android.content.DialogInterface,int)(SourceFile:85)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:935)

Upvotes: 1

Views: 4403

Answers (2)

unlimit
unlimit

Reputation: 3752

Another way as mentioned at https://stackoverflow.com/a/12211522/146167

if (Build.VERSION.SDK_INT > 10)
    builder =  new AlertDialog.Builder(getActivity(), R.style.Theme.Sherlock.Dialog);
else
    builder =  new AlertDialog.Builder(getActivity());

Upvotes: 3

gak
gak

Reputation: 32763

I think I just worked it out, THEME_HOLO_DARK is API level 11. The odd thing is it didn't seem to be a problem when trying it on a device running 2.3.3 (API Level 10).

A method for using it safely is mentioned here: https://groups.google.com/forum/?fromgroups#!topic/actionbarsherlock/0yNlvDXObAo

AlertDialog.Builder builder;
try {
    builder =  new AlertDialog.Builder(getActivity(), R.style.Theme.Sherlock.Dialog);
} catch (NoSuchMethodError e) {
    Log.e(TAG, "Older SDK, using old Builder");
    builder =  new AlertDialog.Builder(getActivity());
}

Also, this is the only part of the code that uses THEME_HOLO_DARK, hence why it's only happening there. I think I was experimenting with different themes at that stage and accidentally left it in there.

Upvotes: 5

Related Questions