android:从Activity跳转到另一个Activity的Fragment的时候程序报错。

以下是报错信息:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.whut.imageencryptionapp, PID: 31941

java.lang.RuntimeException: 
    **Unable to instantiate activity ComponentInfo****{com.whut.imageencryptionapp/com.whut.imageencryptionapp.ui.ImageHandleActivity}: **java.lang.ClassCastExceptio**n: com.whut.imageencryptionapp.ui.ImageHandleActivity **cannot be cast to android.app.Activity       **

    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3678)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3939)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2373)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:213)
    at android.app.ActivityThread.main(ActivityThread.java:8147)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
 Caused by: java.lang.ClassCastException: com.whut.imageencryptionapp.ui.ImageHandleActivity cannot be cast to android.app.Activity
    at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
    at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1257)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3663)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3939) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2373) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:213) 
    at android.app.ActivityThread.main(ActivityThread.java:8147) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 

DeImagePickActivity相关代码如下:

            case R.id.iv_image_show_activity_back:
                /*
                Intent intent = new Intent(this, ImageHandleActivity.class);
                startActivity(intent);

                FragmentManager fragmentManager;
                Fragment fragment = new ImageHandleActivity();
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.rl_fragment1, fragment).commit();

                fragmentTransaction = fragmentManager.beginTransaction();
                ImageHandleActivity imageHandleActivity = new ImageHandleActivity();
                fragmentTransaction.replace(R.id.ll_de, imageHandleActivity).commit();

                 */

                ImageHandleActivity imageHandleActivity = new ImageHandleActivity();
                FragmentManager fragmentManager;
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.rl_fragment1,imageHandleActivity).commit();

                Intent intent = new Intent(this,ImageHandleActivity.class);
                startActivity(intent);

注释中都是我尝试过的方法,但是还是会出现异常。
我要从DeImagePickActivity跳转到ImageHandleActivity,其中ImageHandleActivity继承了Fragment,他的Activity是MainActivity。
感谢解答!

1个回答

startActivity(intent); 中的intent 传入的class是Activity,而你的ImageHandleActivity 是Fragment,所以才会报错ClassCastException类型转换失败,如果你iv_image_show_activity_back 只是一个单纯的返回,只要保证上层activity不销毁,那么finish当前activity就可以了。

cqy980424
金星揽月 直接用finish()就没有报错了!十分感谢!
3 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐