java.lang.NoClassDefFoundError 某些设备出现异常

java.lang.NoClassDefFoundError exception on some devices

我的 Fragment 中有一个 ImageView,正在设置一个 onClick Listener

public class TutorialFragment extends Fragment {

private TutorialAdapter tutorialAdapter;
private TutorialFragment tutorialFragment;
private SplashActivity splashActivity;
private RelativeLayout firstPageRelativeLayout,secondPageRelativeLayout,thirdPageRelativeLayout;
private ImageView backgroundImageView,facebookLoginButton,dekkohLogoImageView;
private ProgressDialogHandler progressDialogHandler;
private AlertDialogHandler alertDialogHandler;
private TextView firstPageTextView,secondPageTextView,thirdPageTextView,facebookPrivacyText,facebookDescriptionText;


@Override
public View onCreateView(LayoutInflater layoutInflater,ViewGroup container, Bundle savedInstanceState) {

    super.onCreateView(layoutInflater,container,savedInstanceState);

    tutorialFragment=this;
    splashActivity = (SplashActivity) getActivity();

    View v = layoutInflater.inflate(R.layout.tutorial_fragment,container,false);
    firstPageRelativeLayout = (RelativeLayout) v.findViewById(R.id.firstScreenRelativeLayout);
    secondPageRelativeLayout = (RelativeLayout) v.findViewById(R.id.secondScreenRelativeLayout);
    thirdPageRelativeLayout = (RelativeLayout) v.findViewById(R.id.thirdScreenRelativeLayout);
    backgroundImageView = (ImageView) v.findViewById(R.id.tutorialImageView);
    facebookLoginButton = (ImageView) v.findViewById(R.id.facebook_login_button);
    dekkohLogoImageView = (ImageView) v.findViewById(R.id.dekkohLogoImage);

    firstPageTextView = (TextView) v.findViewById(R.id.firstPageTextView);
    secondPageTextView = (TextView) v.findViewById(R.id.secondPageTextView);
    thirdPageTextView = (TextView) v.findViewById(R.id.thirdPageTextView);
    facebookPrivacyText = (TextView) v.findViewById(R.id.facebookPrivacyText);
    facebookDescriptionText = (TextView) v.findViewById(R.id.facebookDescriptionText);

    int currentIndex = (int) this.getArguments().getSerializable("tutorial");
    switch (currentIndex){
        case 0:
            firstPageRelativeLayout.setVisibility(View.VISIBLE);
            secondPageRelativeLayout.setVisibility(View.GONE);
            thirdPageRelativeLayout.setVisibility(View.GONE);
            Picasso.with(splashActivity).load(R.drawable.tutorial_1).priority(Picasso.Priority.HIGH).fit().into(backgroundImageView);
            Picasso.with(splashActivity).load(R.drawable.dekkoh_logo_tutorial).priority(Picasso.Priority.HIGH).fit().into(dekkohLogoImageView);
            break;
        case 1:
            firstPageRelativeLayout.setVisibility(View.GONE);
            secondPageRelativeLayout.setVisibility(View.VISIBLE);
            thirdPageRelativeLayout.setVisibility(View.GONE);
            Picasso.with(splashActivity).load(R.drawable.tutorial_2).priority(Picasso.Priority.HIGH).fit().into(backgroundImageView);

            break;
        case 2:
            firstPageRelativeLayout.setVisibility(View.GONE);
            secondPageRelativeLayout.setVisibility(View.GONE);
            thirdPageRelativeLayout.setVisibility(View.VISIBLE);
            Picasso.with(splashActivity).load(R.drawable.tutorial_3).priority(Picasso.Priority.HIGH).fit().into(backgroundImageView);
            Picasso.with(splashActivity).load(R.drawable.fb_login_btn).fit().into(facebookLoginButton);

            break;
    }
    final ArrayList<String> permissions = new ArrayList<String>();
    progressDialogHandler = ProgressDialogHandler.getInstance();
    alertDialogHandler = AlertDialogHandler.getInstance();


    facebookLoginButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // setting facebook permission
                permissions.add("email");
                // permissions.add("user_location");
                permissions.add("user_friends");
                progressDialogHandler.showCustomProgressDialog(splashActivity);

                final JSONObject jb = new JSONObject();

                Session.openActiveSession(splashActivity, true,
                        permissions, new Session.StatusCallback() {

                            @SuppressWarnings("deprecation")
                            @Override
                            public void call(final Session session,
                                             SessionState state, Exception exception) {

                                if (session.isOpened()) {

                                    Request.executeMeRequestAsync(
                                            session,
                                            new Request.GraphUserCallback() {

                                                @Override
                                                public void onCompleted(
                                                        GraphUser user,
                                                        Response response) {
                                                    if (user != null) {


                                                        String emaila = user
                                                                .asMap()
                                                                .get("email")
                                                                .toString();
                                                        String userId = user
                                                                .getId();
                                                        String name1 = user
                                                                .getName();

                                                        try {

                                                            jb.put("provider",
                                                                    "Facebook");
                                                            jb.put("user_id",
                                                                    userId.toString());
                                                            String token = session
                                                                    .getAccessToken()
                                                                    .toString();
                                                            jb.put("token",
                                                                    token);

                                                            Log.d("f b login ",
                                                                    jb.toString());

                                                            // clear seesion
                                                            // details and
                                                            // close
                                                            session.closeAndClearTokenInformation();
                                                            new LoginTheUser(
                                                                    token,
                                                                    user.getId())
                                                                    .execute();

                                                        } catch (JSONException e) {
                                                            // Toast.makeText(getApplicationContext(),
                                                            // e.getLocalizedMessage(),
                                                            // Toast.LENGTH_SHORT).show();
                                                            e.printStackTrace();
                                                        }
                                                    } else {
                                                        progressDialogHandler
                                                                .dismissCustomProgressDialog(splashActivity);
                                                        alertDialogHandler
                                                                .showToast(
                                                                        splashActivity,
                                                                        "Facebook Login Failed!");
                                                    }
                                                }
                                            });
                                } else {
                                }
                            }
                        });

            }
        });

    Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(),"fonts/SortsMillGoudy-Regular.ttf");
    firstPageTextView.setTypeface(typeface);
    secondPageTextView.setTypeface(typeface);
    thirdPageTextView.setTypeface(typeface);
    facebookDescriptionText.setTypeface(typeface);
    facebookPrivacyText.setTypeface(typeface);
    return v;
}

这在 Nexus 和 oneplus 上 运行 没问题,但我在某些设备上遇到以下错误。

 java.lang.NoClassDefFoundError: com.myapp.fragments.TutorialFragment
        at com.myapp.fragments.TutorialFragment.onCreateView(TutorialFragment.java:135)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16497)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

$1表示内class,也就是onclicklistener。如果我注释掉 onclicklistener 代码,Activity 和 Fragment 可以正常启动。为什么是这样?有什么问题?

您的应用似乎达到了 65K 方法的限制,这是 Android 中相当著名的限制。

参考:https://developer.android.com/tools/building/multidex.html#dev-build

您可以按照上面的解决方案link。或者简而言之:

如果您有应用程序 class(您扩展了 class 应用程序),请在 class 中覆盖 attachBaseContext() 并调用 Multidex.install(this)

否则将其添加到您的清单中:

android:name="android.support.multidex.MultiDexApplication"

<application> 标签内。

如果解决了您的问题,请在此回复。它实际上解决了我的问题。

我遇到了这个问题 运行 三星 Android 8.0 设备,但是在三星 Android 9 上没有问题。启用 multidex 和扩展 multidex 应用程序没有帮助。

唯一修复它的方法是将 gradle 工具版本从 3.5.1 降低到 3.4.1 (com.android.tools.build:gradle:3.4.1)