Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java匿名类中捕获抽象异常时出现Android VerifyError_Java_Android_Reflection_Anonymous Class_Verifyerror - Fatal编程技术网

在Java匿名类中捕获抽象异常时出现Android VerifyError

在Java匿名类中捕获抽象异常时出现Android VerifyError,java,android,reflection,anonymous-class,verifyerror,Java,Android,Reflection,Anonymous Class,Verifyerror,我有一些奇怪的错误,我想知道会发生什么 首先,我在Android中使用片段对象的反射。要做到这一点,我必须捕捉反射异常 此代码有效: try { return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity); } catch(NoSuchMethodException e) { return "fail"; }

我有一些奇怪的错误,我想知道会发生什么

首先,我在Android中使用片段对象的反射。要做到这一点,我必须捕捉反射异常

此代码有效:

try
{
    return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
}
catch(NoSuchMethodException e)
{
    return "fail";
}
catch(IllegalAccessException e)
{
    return "fail";
}
catch(InvocationTargetException e)
{
    return "fail";
}
try
{
    return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
}
catch(ReflectiveOperationException e)
{
    return "fail";
}
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
{
    @Override
    public CharSequence getPageTitle(int i)
    {
        try
        {
            return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
        }
        catch(ReflectiveOperationException e)
        {
            return "fail";
        }
    }
});
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
{
    @Override
    public CharSequence getPageTitle(int i)
    {
        try
        {
            return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
        }
        catch(NoSuchMethodException e)
        {
            return "fail";
        }
        catch(IllegalAccessException e)
        {
            return "fail";
        }
        catch(InvocationTargetException e)
        {
            return "fail";
        }
    }
});
但是,由于所有异常都是ReflectiveOperationException的子类,所以我只能创建一个异常处理程序

此代码有效:

try
{
    return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
}
catch(NoSuchMethodException e)
{
    return "fail";
}
catch(IllegalAccessException e)
{
    return "fail";
}
catch(InvocationTargetException e)
{
    return "fail";
}
try
{
    return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
}
catch(ReflectiveOperationException e)
{
    return "fail";
}
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
{
    @Override
    public CharSequence getPageTitle(int i)
    {
        try
        {
            return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
        }
        catch(ReflectiveOperationException e)
        {
            return "fail";
        }
    }
});
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
{
    @Override
    public CharSequence getPageTitle(int i)
    {
        try
        {
            return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
        }
        catch(NoSuchMethodException e)
        {
            return "fail";
        }
        catch(IllegalAccessException e)
        {
            return "fail";
        }
        catch(InvocationTargetException e)
        {
            return "fail";
        }
    }
});
此代码在作为普通Java函数的一部分时有效。然而,当我尝试在匿名类中使用它时,事情变得很棘手

此代码不起作用:

try
{
    return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
}
catch(NoSuchMethodException e)
{
    return "fail";
}
catch(IllegalAccessException e)
{
    return "fail";
}
catch(InvocationTargetException e)
{
    return "fail";
}
try
{
    return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
}
catch(ReflectiveOperationException e)
{
    return "fail";
}
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
{
    @Override
    public CharSequence getPageTitle(int i)
    {
        try
        {
            return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
        }
        catch(ReflectiveOperationException e)
        {
            return "fail";
        }
    }
});
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
{
    @Override
    public CharSequence getPageTitle(int i)
    {
        try
        {
            return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
        }
        catch(NoSuchMethodException e)
        {
            return "fail";
        }
        catch(IllegalAccessException e)
        {
            return "fail";
        }
        catch(InvocationTargetException e)
        {
            return "fail";
        }
    }
});
它编译得很顺利,但当应用程序达到这一行时,我得到了一个验证错误

然而,如果我不使用ReflectiveOperationException并求助于具体的类型,事情就会再次发生

此代码再次起作用:

try
{
    return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
}
catch(NoSuchMethodException e)
{
    return "fail";
}
catch(IllegalAccessException e)
{
    return "fail";
}
catch(InvocationTargetException e)
{
    return "fail";
}
try
{
    return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
}
catch(ReflectiveOperationException e)
{
    return "fail";
}
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
{
    @Override
    public CharSequence getPageTitle(int i)
    {
        try
        {
            return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
        }
        catch(ReflectiveOperationException e)
        {
            return "fail";
        }
    }
});
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
{
    @Override
    public CharSequence getPageTitle(int i)
    {
        try
        {
            return (String) MyFragment.class.getMethod("aStaticMethod", new Class[]{MyActivity.class} ).invoke(null, myActivity);
        }
        catch(NoSuchMethodException e)
        {
            return "fail";
        }
        catch(IllegalAccessException e)
        {
            return "fail";
        }
        catch(InvocationTargetException e)
        {
            return "fail";
        }
    }
});

我的问题是:发生了什么?这是Java或Android中的某种错误,还是我做了非法的事情?

ReflectiveOperationException
添加到API 19(KitKat)中,在API 19之前的任何设备上使用它都会导致
验证错误
,因为该类不存在