我是Android的新手。
要重用和封装,我编写了一个BaseIntent
类,该类扩展了Intent
类以记录哪个类调用了我的活动,并重写了startActivity
和getIntent
以使用我的活动BaseIntent
。但是,当我从一个Activity
实例开始一个BaseIntent
并想使用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);
}
}
答案 0 :(得分:0)
我必须更详细地研究一下,但该错误可能是由于super.getIntent()
返回了不是Intent
实例的BaseIntent
而引起的。我不知道为什么。堆栈跟踪(完整错误)将有助于确认这一点并找出根本原因。
查看堆栈跟踪,尽管缺少一些代码,但我认为问题在于,当您从LaunchActivity#onCreate
调用LoginActivity#onCreate
时,您从未指定Intent
,导致它使用内置的,而不是BaseIntent
的实例。