AlertDialog.Builder中的java.lang.NoClassDefFoundError

时间:2016-03-11 18:28:30

标签: android android-alertdialog noclassdeffounderror

我一次又一次地得到这个错误,即使我已经使用了" multiDexEnabled true"在我的gradle.Here是我的java文件,我收到错误:

package com.itjobnotification.first.app;

        import android.content.Intent;
        import android.graphics.Color;
        import android.preference.PreferenceManager;
        import android.provider.Settings;
        import android.content.Context;
        import android.database.DataSetObserver;
        import android.support.design.widget.TextInputLayout;
        import android.support.v7.app.ActionBar;
        import android.support.v7.app.AppCompatActivity;
        import android.support.v7.widget.CardView;
        import android.support.v7.widget.Toolbar;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.view.Window;
        import android.view.WindowManager;
        import android.view.inputmethod.InputMethodManager;

        import android.widget.ListAdapter;

        import android.support.v7.app.ActionBarActivity;
        import android.os.Bundle;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.EditText;
        import android.net.Uri;
        import android.util.Log;

        import com.gc.materialdesign.views.Button;
        import com.gc.materialdesign.widgets.Dialog;
        import com.google.gson.Gson;

        import android.content.SharedPreferences;
        import android.widget.CheckBox;

        import java.util.ArrayList;
        import java.util.Arrays;

        import android.app.AlertDialog;
        import android.content.DialogInterface;
        import android.widget.Spinner;
        import android.widget.SpinnerAdapter;
        import android.widget.Toast;
        import android.widget.ArrayAdapter;

        import android.widget.TextView;
        import android.text.method.LinkMovementMethod;
        import android.text.Html;


        public class signupActivity extends AppCompatActivity implements OnTaskCompleted {
            AlertDialog dialog;
            com.gc.materialdesign.views.ButtonFlat skills;
            ArrayList<String> selectedItems;
            Spinner ctc;
            private Toolbar toolbar;
            formBody fb;
            int index = 0, i = 0;
            String[] ar = new String[10];

            String item;
            Button submit;
            EditText name, email, company, exp, promo, location;
            CardView cv;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_signup);

                toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
                setSupportActionBar(toolbar);
                getSupportActionBar().setTitle("Get Started!!");


                skills = (com.gc.materialdesign.views.ButtonFlat) findViewById(R.id.chooseSkills);
                TextView textView = (TextView) findViewById(R.id.terms);
                submit = (Button) findViewById(R.id.submit);
                Spinner spinner = (Spinner) findViewById(R.id.college);
                Spinner spinner1 = (Spinner) findViewById(R.id.loc);
                name = (EditText) findViewById(R.id.name);
                email = (EditText) findViewById(R.id.email);
                company = (EditText) findViewById(R.id.company);
                exp = (EditText) findViewById(R.id.exp);
                ctc = (Spinner) findViewById(R.id.ctc);
                CardView cv = (CardView) findViewById(R.id.card_view);

                final formBody fb = new formBody();

                // first spinner - college
                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                        R.array.college, android.R.layout.simple_spinner_item);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner.setAdapter(
                        new NothingSelectedSpinnerAdapter(
                                adapter,
                                R.layout.contact_spinner_row_nothing_selected,
                                this));


                //second spinner with job location
                ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,
                        R.array.location, android.R.layout.simple_spinner_item);
                adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner1.setAdapter(
                        new NothingSelectedSpinnerAdapter(
                                adapter1,
                                R.layout.contact_spinner_row_nothing_selected_loc,
                                this));

                ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
                        R.array.category, android.R.layout.simple_spinner_item);
                adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                ctc.setAdapter(
                        new NothingSelectedSpinnerAdapter(
                                adapter2,
                                R.layout.contact_spinner_row_nothing_selected_ctc,
                                this));
                ;


                // skill button, dialog opens with the options when clicked
                skills.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        final CharSequence[] items = new String[]{"Java", "C/C++", "Analytics", "Android", "Distributed Systems", "UI", "Python", "Ruby"};
                        selectedItems = new ArrayList<String>();
                        selectedItems.add("Java");
                        selectedItems.add("C/C++");
                        selectedItems.add("Analytics");
                        selectedItems.add("Android");
                        selectedItems.add("Distributed Systems");
                        selectedItems.add("UI");
                        selectedItems.add("Python");
                        selectedItems.add("Ruby");
                        AlertDialog.Builder dbuilder = new AlertDialog.Builder(signupActivity.this)

                       .setTitle("Select your skills")
                        .setMultiChoiceItems(items, null,
                                new DialogInterface.OnMultiChoiceClickListener() {
                                    // indexSelected contains the index of item (of which checkbox checked)
                                    @Override
                                    public void onClick(DialogInterface dialog, int indexSelected,
                                                        boolean isChecked) {
                                        if (isChecked) {
                                            item =  selectedItems.get(indexSelected);
                                            Log.d("hello123", item);
                                            ar[index++] = item;
                                        } else if (selectedItems.contains(indexSelected)) {
                                            selectedItems.remove(Integer.valueOf(indexSelected));
                                        }
                                    }
                                })

                                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int id) {
                                    }
                                })
                                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });
                        dialog = dbuilder.create();//AlertDialog dialog; create like this outside onClick
                        dialog.show();
                    }
                });

                submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Uri.Builder builder = new Uri.Builder();
                        builder.scheme("http");
                        builder.authority(QuickstartPreferences.SERVER_IP);
                        builder.path(QuickstartPreferences.SIGNUP_PAGE);

                        fb.name = name.getText().toString();
                        //      fb.ctc = ctc.getText().toString();
                        fb.exp = exp.getText().toString();
                        fb.email = email.getText().toString();
                        fb.company = company.getText().toString();
                        //   fb.location = location.getText().toString();

                        if (checkEmpty(fb.name, "Name can't be empty")) {
                            return;
                        }
                        if (checkEmpty(fb.email, "Email can't be empty")) {
                            return;
                        }
                        if (checkEmpty(fb.company, "Company can't be empty")) {
                            return;
                        }
                        if (checkEmpty(fb.exp, "Experience can't be empty")) {
                            return;
                        }
                        if (fb.ctc.isEmpty()) {
                            fb.ctc = "0";
                        }

                        Spinner spinner1 = (Spinner) findViewById(R.id.loc);
                        String str = null;
                        try {
                            str = spinner1.getSelectedItem().toString();
                        } catch (Exception e) {
                            Toast.makeText(getApplicationContext(), "Please select job location",
                                    Toast.LENGTH_LONG).show();
                            return;
                        }
                        fb.location = str;

                        Spinner spinner = (Spinner) findViewById(R.id.college);
                        String cg = null;
                        try {
                            cg = spinner.getSelectedItem().toString();
                        } catch (Exception e) {
                            Toast.makeText(getApplicationContext(), "Please select college",
                                    Toast.LENGTH_LONG).show();
                            return;
                        }
                        fb.college = cg;
                        if (index > 0) {
                            fb.skill_set = Arrays.copyOfRange(ar, 0, index);
                        } else {
                            Toast.makeText(getApplicationContext(), "Skills can't be empty",
                                    Toast.LENGTH_LONG).show();
                            return;
                        }


                        String android_id = Settings.Secure.getString(getContentResolver(),
                                Settings.Secure.ANDROID_ID);
                        fb.device_id = android_id;

                        Gson gson = new Gson();
                        String postdata = gson.toJson(fb);
                        String myUrl = builder.build().toString();
                        new Dbtask(signupActivity.this, postdata).execute(myUrl);
                    }
                });

                // terms and conditions textView
                textView.setClickable(true);
                textView.setMovementMethod(LinkMovementMethod.getInstance());
                String text = "I agree to <a href='http://itjobnotification.com/terms.html'> terms and conditions. </a>";
                textView.setText(Html.fromHtml(text));
                textView.setTextSize(10);

                Dialog dialog = new Dialog(this, "Prompt!!", "Just add few details of you and get started");
                dialog.show();


            }

            public boolean checkEmpty(String str, String message) {
                if (str.isEmpty()) {
                    Toast.makeText(getApplicationContext(), message,
                            Toast.LENGTH_LONG).show();
                    return true;
                }
                return false;

            }

            @Override
            public void onTaskCompleted(String str) {
                //Log.e("nimish", "signup completed");
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
                sharedPreferences.edit().putBoolean(QuickstartPreferences.SIGNUP, true).apply();
                Intent intent = new Intent(this, RegistrationIntentService.class);
                startService(intent);
                new AlertDialog.Builder(this)
                        .setTitle("Thanks for Signing up !!!")
                        .setMessage("We will notify you when we find suitable jobs for you")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                Log.d("hello", String.valueOf(QuickstartPreferences.SIGNUP));
                                finish();
                            }
                        })
                        .setIcon(android.R.drawable.btn_star)
                        .show();
                   /* Dialog dialog = new Dialog(this,"Thanks for Signing up !!!", "We will notify you when we find suitable jobs for you");
                    dialog.setCancelable(false);
                    dialog.setCanceledOnTouchOutside(false);
                    dialog.setOnAcceptButtonClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    });
                    dialog.show();  */
            }

            @Override
            public void onResume() {  // After a pause OR at startup
                super.onResume();
                //Log.e("nimish", "refresh");

            }

            public class NothingSelectedSpinnerAdapter implements SpinnerAdapter, ListAdapter {
                protected static final int EXTRA = 1;
                protected SpinnerAdapter adapter;
                protected Context context;
                protected int nothingSelectedLayout;
                protected int nothingSelectedDropdownLayout;
                protected LayoutInflater layoutInflater;


                public NothingSelectedSpinnerAdapter(
                        SpinnerAdapter spinnerAdapter,
                        int nothingSelectedLayout, Context context) {
                    this(spinnerAdapter, nothingSelectedLayout, -1, context);
                }


                public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter, int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) {
                    this.adapter = spinnerAdapter;
                    this.context = context;
                    this.nothingSelectedLayout = nothingSelectedLayout;
                    this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
                    layoutInflater = LayoutInflater.from(context);
                }

                @Override
                public final View getView(int position, View convertView, ViewGroup parent) {
                    // This provides the View for the Selected Item in the Spinner, not
                    // the dropdown (unless dropdownView is not set).
                    if (position == 0) {
                        return getNothingSelectedView(parent);
                    }
                    return adapter.getView(position - EXTRA, null, parent); // Could re-use
                    // the convertView if possible.
                }


                protected View getNothingSelectedView(ViewGroup parent) {
                    return layoutInflater.inflate(nothingSelectedLayout, parent, false);
                }

                @Override
                public View getDropDownView(int position, View convertView, ViewGroup parent) {
                    // Android BUG! http://code.google.com/p/android/issues/detail?id=17128 -
                    // Spinner does not support multiple view types
                    if (position == 0) {
                        return nothingSelectedDropdownLayout == -1 ?
                                new View(context) :
                                getNothingSelectedDropdownView(parent);
                    }
                    // Could re-use the convertView if possible, use setTag...
                    return adapter.getDropDownView(position - EXTRA, null, parent);
                }


                protected View getNothingSelectedDropdownView(ViewGroup parent) {
                    return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false);
                }

                @Override
                public int getCount() {
                    int count = adapter.getCount();
                    return count == 0 ? 0 : count + EXTRA;
                }

                @Override
                public Object getItem(int position) {
                    return position == 0 ? null : adapter.getItem(position - EXTRA);
                }

                @Override
                public int getItemViewType(int position) {
                    return 0;
                }

                @Override
                public int getViewTypeCount() {
                    return 1;
                }

                @Override
                public long getItemId(int position) {
                    return position >= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA;
                }

                @Override
                public boolean hasStableIds() {
                    return adapter.hasStableIds();
                }

                @Override
                public boolean isEmpty() {
                    return adapter.isEmpty();
                }

                @Override
                public void registerDataSetObserver(DataSetObserver observer) {
                    adapter.registerDataSetObserver(observer);
                }

                @Override
                public void unregisterDataSetObserver(DataSetObserver observer) {
                    adapter.unregisterDataSetObserver(observer);
                }

                @Override
                public boolean areAllItemsEnabled() {
                    return false;
                }

                @Override
                public boolean isEnabled(int position) {
                    return position != 0; // Don't allow the 'nothing selected'
                    // item to be picked.
                }

            }
        }

这是在AlertDialog.Builder中显示错误的logcat:

 FATAL EXCEPTION: main
                Process: com.itjobnotification.first.app, PID: 3403
                java.lang.NoClassDefFoundError: com.itjobnotification.first.app.signupActivity$1$3
                at com.itjobnotification.first.app.signupActivity$1.onClick(signupActivity.java:142)
                at com.gc.materialdesign.views.ButtonFlat.onDraw(ButtonFlat.java:83)

`

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.itjobnotification.first.app" >

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".SplashActivity"
                android:theme="@android:style/Theme.Black.NoTitleBar" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

            <meta-data
                android:name="com.google.android.gms.version"
                android:theme="@style/AppTheme"
                android:value="@integer/google_play_services_version" />

            <activity
                android:name=".signupActivity"
                android:configChanges="orientation|keyboardHidden"
                android:label="Get Started!!"
                android:theme="@style/AppTheme"
                android:windowSoftInputMode="adjustResize|stateHidden" >
            </activity>

            <receiver
                android:name="com.google.android.gms.gcm.GcmReceiver"
                android:exported="true"
                android:permission="com.google.android.c2dm.permission.SEND"
                android:theme="@style/AppTheme" >
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                </intent-filter>
            </receiver>

            <service
                android:name=".RegistrationIntentService"
                android:exported="false"
                android:theme="@style/AppTheme" >
            </service>
            <service
                android:name=".MyGcmListenerService"
                android:exported="false"
                android:theme="@style/AppTheme" >
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                </intent-filter>
            </service>

            <activity
                android:name=".jobshow"
                android:label="@string/title_activity_jobshow"
                android:theme="@style/DarkAction" >
            </activity>
            <activity
                android:name=".list"
                android:label="@string/title_activity_list"
                android:theme="@style/AppTheme" >
            </activity>
            <activity
                android:name=".Test1Activity"
                android:label="@string/title_activity_test1"
                android:theme="@style/AppTheme" >
            </activity>
            <activity
                android:name=".chatActivity"
                android:label="@string/title_activity_chat"
                android:theme="@style/AppTheme" >
            </activity>
            <activity
                android:name=".quiz"
                android:label="@string/title_activity_quiz"
                android:theme="@style/AppTheme" >
            </activity>

            <meta-data
                android:name="com.facebook.sdk.ApplicationId"
                android:value="@string/facebook_app_id" />

            <activity
                android:name="com.facebook.FacebookActivity"
                android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
                android:label="@string/app_name"
                android:theme="@android:style/Theme.Translucent.NoTitleBar" />
            <!-- Used for Google Play Store Campaign Measurement -->
            <service android:name="com.google.android.gms.analytics.CampaignTrackingService" />

            <receiver
                android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
                android:exported="true" >
                <intent-filter>
                    <action android:name="com.android.vending.INSTALL_REFERRER" />
                </intent-filter>
            </receiver>


        </application>

    </manifest>

2 个答案:

答案 0 :(得分:1)

  1. 在清单代码中添加以下行:

     <application
    
    android:name="android.support.multidex.MultiDexApplication">
    
        </application>
    
  2. 您还必须将以下内容添加到gradle文件依赖项中:

    编译'com.android.support:multidex:1.0.0'

  3. 最后您还需要在OnCreate方法中将以下内容添加到您的java文件中:

    MultiDex.install(本);

  4. 更多参考资料查看此链接: https://developer.android.com/tools/building/multidex.html

答案 1 :(得分:0)

如果项目中的方法超过65k,则Multidex会创建多个dex文件。如果库需要它,那么您需要使用它。如果你在build.gradle中定义它,你应该没问题。无需以编程方式添加它。

在build.gradle文件中

1.增加

dependencies {
    compile 'com.android.support:multidex:1.0.0'
}

2。然后添加

android {
defaultConfig {
    multiDexEnabled true
    }
}

3。然后添加

<activity> 
    com.itjobnotification.first.app.signupactivity 
</activity>

使用包括包名在内的全名清单。并使类名称以SignUpActivity开头。