java.lang.ClassCastException: 我无法将超对象转换为子对象

java.lang.ClassCastException: I cannot cast a superobject to a subobject

我是 Android 的新手。

为了重用和封装,我写了一个 BaseIntent class 扩展了 Intent class 来记录哪个 class 调用了我的 activity,并重写 startActivitygetIntent 以使用我的 BaseIntent。但是当我用一个 BaseIntent 实例启动一个 Activity 并想使用 getIntent 来获得一个 BaseIntent 对象时,我得到了一个 ClassCastException。 我在 Github 中的回购:https://github.com/LeoFate/MusicPlayer 这是我的代码:

 public abstract class BaseActivity extends AppCompatActivity {
    private int layout;
    protected String callClass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        callClass = getIntent().getCallClass();
        setContentView(layout);
        initView();
    }

    public abstract void initView();

    public void setLayout(int layout) {
        this.layout = layout;
    }

    public BaseIntent getIntent() {
        return (BaseIntent) super.getIntent();
    }

    public void startActivity(BaseIntent intent) {
        super.startActivity(intent);
    }
}
public class BaseIntent extends Intent {
    private final String KEY = "ClassName";

    public BaseIntent(Context packageContext, Class<?> cls) {
        super(packageContext, cls);
        putExtra(KEY, packageContext.getClass().getName());
    }

    String getCallClass() {
        return getStringExtra(KEY);
    }
}

public class LaunchActivity extends AppCompatActivity {
...
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseIntent intent = new BaseIntent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
public class LoginActivity extends BaseActivity implements LoginContact.View {
...
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        setLayout(R.layout.activity_login);
        super.onCreate(savedInstanceState);
    }
2019-05-03 10:45:34.850 20582-20582/com.example.admin.cloudmusic E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.admin.cloudmusic, PID: 20582
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.cloudmusic/com.example.admin.cloudmusic.Login.LoginActivity}: java.lang.ClassCastException: android.content.Intent cannot be cast to com.example.admin.cloudmusic.Base.BaseIntent
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3303)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
     Caused by: java.lang.ClassCastException: android.content.Intent cannot be cast to com.example.admin.cloudmusic.Base.BaseIntent
        at com.example.admin.cloudmusic.Base.BaseActivity.getIntent(BaseActivity.java:25)
        at com.example.admin.cloudmusic.Base.BaseActivity.getIntent(BaseActivity.java:6)
        at android.app.HwActivitySplitterImpl.getDefault(HwActivitySplitterImpl.java:264)
        at huawei.android.common.HwFrameworkFactoryImpl.getHwActivitySplitterImpl(HwFrameworkFactoryImpl.java:618)
        at huawei.android.common.HwFrameworkFactoryImpl.getHwActivitySplitterImpl(HwFrameworkFactoryImpl.java:614)
        at android.common.HwFrameworkFactory.getHwActivitySplitterImpl(HwFrameworkFactory.java:945)
        at android.app.HwCustActivityImpl.isRequestSplit(HwCustActivityImpl.java:42)
        at android.app.Activity.onCreate(Activity.java:1062)
        at android.support.v4.app.SupportActivity.onCreate(ComponentActivity.java:75)
        at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:335)
        at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
        at com.example.admin.cloudmusic.Base.BaseActivity.onCreate(BaseActivity.java:12)
        at com.example.admin.cloudmusic.Login.LoginActivity.onCreate(LoginActivity.java:34)
        at android.app.Activity.performCreate(Activity.java:7383)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411) 
        at android.app.ActivityThread.-wrap12(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994) 
        at android.os.Handler.dispatchMessage(Handler.java:108) 
        at android.os.Looper.loop(Looper.java:166) 
        at android.app.ActivityThread.main(ActivityThread.java:7529) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 
2019-05-03 10:45:34.869 20582-20582/com.example.admin.cloudmusic I/Process: Sending signal. PID: 20582 SIG: 9

我的解决方案:

public class BaseIntent {
    private static final String KEY = "ClassName";

    public static Intent get(Context packageContext, Class<?> cls) {
        Intent intent = new Intent(packageContext, cls);
        intent.putExtra(KEY, packageContext.getClass().getName());
        return intent;
    }

    static String getCallClass(Intent intent) {
        return intent.getStringExtra(KEY);
    }
}

我必须更详细地查看一下,但错误可能是由于 super.getIntent() 返回的 Intent 不是 BaseIntent 的实例造成的.我不确定为什么。堆栈跟踪(完整错误)将有助于确认这一点并确定根本原因。


查看堆栈跟踪,虽然缺少一些代码,但我认为问题在于当您从 LoginActivity#onCreate 调用 LaunchActivity#onCreate 时,您从未指定 Intent,导致它使用内置的,它不是 BaseIntent.

的实例