简体   繁体   English

实例化片段时出现java.lang.VerifyError

[英]java.lang.VerifyError while instantiating a fragment

In android versions earlier than Lollipop, I keep getting the VerifyError while trying to instantiate a fragment, leading to an app crash. 在Lollipop之前的android版本中,在尝试实例化片段时,我不断收到VerifyError,导致应用崩溃。 Can someone explain to me the cause of this error? 有人可以向我解释此错误的原因吗?

12-19 17:46:52.510  28238-28238/com.greeblu.tootl2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.greeblu.tootl2, PID: 28238
java.lang.VerifyError: com/greeblu/tootl2/fragment/ProfileGridFragment
        at com.greeblu.tootl2.activity.ProfileActivity.launchGridFragment(ProfileActivity.java:289)
        at com.greeblu.tootl2.fragment.ProfileOverlayFragment$3.onClick(ProfileOverlayFragment.java:123)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18439)
        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:5034)
        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:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
        at dalvik.system.NativeStart.main(Native Method)

This is how I'm calling the fragment for older android versions: 这就是我为旧版Android调用片段的方式:

FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.profileContainer, new ProfileGridFragment(),fragmentTag); //Replaces the Fragment C previously in the right_container with a new Fragment B
        ft.commit();

Edit: The code for the onClick call is as follows. 编辑: onClick调用的代码如下。 It is called from another fragment in the same activity 从同一活动中的另一个片段调用

picContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((ProfileActivity) getActivity()).launchGridFragment(overlayDp, "all");
        }
    });

I've encountered this exact error because I had a try-catch block with exception class that is unavailable on old android versions. 我遇到了这个确切的错误,因为我有一个带有异常类的try-catch块,在旧的android版本上不可用。 The block wasn't even in the constructor, it was in one of the onClick methods so the code wasn't even executed, but nevertheless app was always crashing with VerifyError as soon as I was trying to create the fragment. 该块甚至不在构造函数中,而是在onClick方法之一中,因此甚至没有执行该代码,但是,一旦我尝试创建该片段,应用程序始终会由于VerifyError崩溃。

Solver it by changing clause block with instanceof check: 通过使用instanceof check更改子句块来解决它:

Crashing: 崩溃:

try {
    ...
} catch (FileUriExposedException e) {
    ...
}

Works ok: 可以正常工作:

try {
    ...
} catch (Exception e) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && e instanceof FileUriExposedException) {
        ...
    }
}

Are you using the support Fragment class from android.support.v4.app.Fragment? 您是否正在使用android.support.v4.app.Fragment中的support Fragment类? If so you need to be calling 如果是这样,您需要致电

getSupportFragmentManager()
instead of 代替
getFragmentManager()

Note that the Fragment class from android.app.Fragment is now deprecated. 请注意,不推荐使用android.app.Fragment中的Fragment类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM