Я новичок в 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);
}
}