[java.lang.NoClassDefFoundError在某些设备上异常

问题描述 投票:4回答:2

我的ImageView中有一个Fragment,正在设置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$1
        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表示内部类,即onclicklistener。如果我注释掉onclicklistener代码,则Activity和Fragment启动正常。为什么是这样?有什么问题?

java android android-fragments onclicklistener samsung-mobile
2个回答
16
投票

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

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

您可以按照上面链接中的解决方法进行操作。或简而言之:

如果您有Application类(扩展了Application类),则在该类中重写attachBaseContext()并调用Multidex.install(this)

否则将其添加到清单中:

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

<application>标签内部。

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


0
投票

我在运行Samsung Android 8.0设备时遇到此问题,但是在Samsung Android 9上没有问题。启用multidex和扩展multidex应用程序无济于事。

唯一解决此问题的方法是将Gradle工具的版本从3.5.1减少到3.4.1(com.android.tools.build:gradle:3.4.1)

热门问题
推荐问题
最新问题